1. 程式人生 > >idea中原生Servlet3.0開發之---使用配置類和註解的方式整合SpringMVC

idea中原生Servlet3.0開發之---使用配置類和註解的方式整合SpringMVC

以註解方式來啟動SpringMVC前需瞭解的基礎知識:

1、web容器在啟動的時候,會掃描每個jar包下的META-INF/services/javax.servlet.ServletContainerInitializer


2、載入這個檔案指定的類SpringServletContainerInitializer


3、spring的應用一啟動會載入感興趣的WebApplicationInitializer介面的下的所有元件;

  
4、並且為WebApplicationInitializer元件建立物件(元件不是介面,不是抽象類)
    1)、AbstractContextLoaderInitializer:建立根容器;createRootApplicationContext();
    2)、AbstractDispatcherServletInitializer:
            建立一個web的ioc容器;createServletApplicationContext();
            建立了DispatcherServlet;createDispatcherServlet();
            將建立的DispatcherServlet新增到ServletContext中;
                getServletMappings();
    3)、AbstractAnnotationConfigDispatcherServletInitializer:註解方式配置的DispatcherServlet初始化器
            建立根容器:createRootApplicationContext()
                    getRootConfigClasses();傳入一個配置類
            建立web的ioc容器: createServletApplicationContext();
                    獲取配置類;getServletConfigClasses();
===========================    
總結:


    以註解方式來啟動SpringMVC;繼承AbstractAnnotationConfigDispatcherServletInitializer;
實現抽象方法指定DispatcherServlet的配置資訊;

===========================
定製SpringMVC;
1)、@EnableWebMvc:開啟SpringMVC定製配置功能;相當於web.xml中配置的 <mvc:annotation-driven/>;

2)、配置元件(檢視解析器、檢視對映、靜態資源對映、攔截器。。。)
    extends WebMvcConfigurerAdapter

===========================

springmvc父子容器

各自負責一些掃描的包

父容器:功能類的包,如:Controller,前端控制器,檢視解析器

子容器:業務模組的包,如:service,repositories,資料庫事物

使用配置類和註解的方式整合SpringMVC的例項步驟:

一、匯入servlet和springmvc的jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.atguigu</groupId>
  <artifactId>springmvc-annotation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <dependencies>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-webmvc</artifactId>
  		<version>4.3.11.RELEASE</version>
  	</dependency>
  	
  	<dependency>
  		<groupId>javax.servlet</groupId>
  		<artifactId>servlet-api</artifactId>
  		<version>3.0-alpha-1</version>
  		<scope>provided</scope>
  	</dependency>
  </dependencies>

  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-war-plugin</artifactId>
  			<version>2.4</version>
  			<configuration>
  				<failOnMissingWebXml>false</failOnMissingWebXml>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
</project>

二、建立MyWebAppInitializer .java相當於web.xml檔案

並繼承抽象類AbstractAnnotationConfigDispatcherServletInitializer(具體可看博文開始時描述)

此類相當於web.xml;載入spring和springmvc,並設定攔截

package com.atguigu;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import com.atguigu.config.AppConfig;
import com.atguigu.config.RootConfig;

//web容器啟動的時候建立物件;呼叫方法來初始化容器以前前端控制器
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	//獲取根容器的配置類;(Spring的配置檔案)   父容器;
	@Override
	protected Class<?>[] getRootConfigClasses() {
		// TODO Auto-generated method stub
		return new Class<?>[]{RootConfig.class};
	}

	//獲取web容器的配置類(SpringMVC配置檔案)  子容器;
	@Override
	protected Class<?>[] getServletConfigClasses() {
		// TODO Auto-generated method stub
		return new Class<?>[]{AppConfig.class};
	}

	//獲取DispatcherServlet的對映資訊
	//  /:攔截所有請求(包括靜態資源(xx.js,xx.png)),但是不包括*.jsp;
	//  /*:攔截所有請求;連*.jsp頁面都攔截;jsp頁面是tomcat的jsp引擎解析的;
	@Override
	protected String[] getServletMappings() {
		// TODO Auto-generated method stub
		return new String[]{"/"};
	}

}

 三、建立spring的配置檔案RootConfig.java,即父容器

package com.atguigu.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

//Spring的容器不掃描controller;父容器
@ComponentScan(value="com.atguigu",excludeFilters={
		@Filter(type=FilterType.ANNOTATION,classes={Controller.class})
})
public class RootConfig {

}

四、建立springmvc的配置檔案AppConfig.java,即子容器

package com.atguigu.config;


import java.util.List;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;


//SpringMVC只掃描Controller;子容器
//useDefaultFilters=false 禁用預設的過濾規則;
@ComponentScan(value="com.atguigu",includeFilters={
		@Filter(type=FilterType.ANNOTATION,classes={Controller.class})
},useDefaultFilters=false)
public class AppConfig {


}

五、 框架搭建好後,建立controller和service等業務程式碼,進行測試

HelloController.java

package com.atguigu.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.atguigu.service.HelloService;

@Controller
public class HelloController {
	
	@Autowired
	HelloService helloService;
	
	
	@ResponseBody
	@RequestMapping("/hello")
	public String hello(){
		String hello = helloService.sayHello("tomcat..");
		return hello;
	}

}

HelloService.java

package com.atguigu.service;

import org.springframework.stereotype.Service;

@Service
public class HelloService {
	
	public String sayHello(String name){
		
		return "Hello "+name;
	}

}

 六、執行tomcat測試

說明:controller能訪問到,並且能呼叫service,說明springmvc配置成功