spring in action学习笔记十五:配置DispatcherServlet和ContextLoaderListener的几种方式。

   在spring in action中论述了:DispatcherServlet和ContextLoaderListener的关系,简言之就是DispatcherServlet是用于加载web层的组件的上下文。ContextLoadListener是加载

其他组件的上下文。

第一种方式:纯注解的方式:

在spring4.0版本以上,倾向用注解的方式配置DispatcherServlet和ContextLoaderListener.配置方式如下:

思路:一个类只要继承AbstractAnnotationConfigDispatcherServletInitializer就会自动配置DispatcherServlet和ContextLoaderListener.

代码如下:

WebConfig的代码如下:

 package spittr.config;

 import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.SpringServletContainerInitializer;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.BeanNameViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import spittr.web.HomeController; import javax.servlet.ServletContainerInitializer;
import javax.servlet.http.Part;
import java.io.IOException; /**
* Created by ${秦林森} on 2017/6/12.
*/
@Configuration
@EnableWebMvc//这个注解不能少。
@ComponentScan(basePackages = {"spittr.web"})
public class WebConfig extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
//设置前缀
viewResolver.setPrefix("/WEB-INF/views/");
//设置后缀
viewResolver.setSuffix(".jsp");
viewResolver.setViewClass(JstlView.class);
viewResolver.setExposeContextBeansAsAttributes(true);
return viewResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();//configure static content handling.
}
@Bean
public MultipartResolver multipartResolver() throws IOException{
return new StandardServletMultipartResolver();
}
@Bean
public MultipartResolver commonsMultipartResolver() throws IOException{
CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver();
//setting location where file upload to.
multipartResolver.setUploadTempDir(new FileSystemResource("com/qls/sen"));
multipartResolver.setMaxUploadSize(10*1024*1024);
return multipartResolver;
}
}

RootConfig的代码如下:

 package spittr.config;

 import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; /**
* Created by ${秦林森} on 2017/6/12.
*/
@Configuration
/**
* excludeFilters是指明不被@ComponentScan扫描的类型
*/
@ComponentScan(basePackages = {"spittr"},excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class)}) public class RootConfig { }
 package spittr.config;

 import com.myapp.config.MyFilter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; import javax.servlet.*; /**
* Created by ${秦林森} on 2017/6/12.
*/
public class SpittrWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
//这个方法是配置ContextLoadListener.
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfig.class};
}
//这个方法是配置DispatcherServlet
@Override
protected Class<?>[] getServletConfigClasses() {
//specifiy configuration class
return new Class<?>[]{WebConfig.class};
} @Override
protected String[] getServletMappings() {
return new String[]{"*.do"};//Map DispatcherServlet to
}
//文件上传。
@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
registration.setMultipartConfig(new MultipartConfigElement("/tmp/spittr/upload"));
registration.setLoadOnStartup(1);
registration.setInitParameter("contextConfigLocation","/WEB-INF/spring/root-context.xml");
} /**
* 在DispatcherServlet中设置过滤器。
* @return
*/
@Override
protected Filter[] getServletFilters() {
return new Filter[] {(Filter) new MyFilter(),
new CharacterEncodingFilter("utf-8")};
} @Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addServlet("m","");
} }

第二种方式:纯配置文件的方式:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- ContextLoaderListener所加载的bean。相当于第一种方式里面的RootConfig类中包扫描的bean。-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>spring-julysecond.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- DispatcherServlet中加载的配置文件。-->
<param-name>contextConfigLocation</param-name>
<param-value>com/config/spring-julythird.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

第三种方式:注解和xml配置的混合方式。

在混合方式中必须要让DispatcherServlet和ContextLoadListener知道你在xml中用了注解。用AnnotationConfigWebApplicationContext告诉他俩即可。

代码如下:注意这里的代码是:

web-app用的是:

web-app_2_5.xsd而不是3.1版本的。否则会在
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>报错。

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- 告诉ContextLoadListener知道你用注解的方式加载bean。-->
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<!-- ContextLoaderListener加载RootConfig的bean-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>spittr.config.RootConfig</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param> <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>spittr.config.WebConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
上一篇:Linux学习日记-WCF RestFul的部署(三)


下一篇:jdeveloper 恢复默认配置