本文要介绍用注解方式代替web.xml与SpringMVC的配置文件
实现步骤
创建一个初始化配置类,继承AbstractAnnotationConfigDispatcherServletInitializer,重写里面的三个方法。第一个方法getRootConfigClasses获取spring指定的配置类,用来返回spring配置类的一个数组;第二个方法getServletConfigClasses获取springMVC指定的配置类,用来返回springMVC配置类的一个数组;第三个方法getServletMappings指定DispatcherServlet的映射路径,就是设置url-pattern,返回的是url-pattern配置的值。
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* 指定spring配置类
* @return
*/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
/**
* 指定SpringMVC的配置类
* @return
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
/**
* 指定DispatcherServlet的映射路径
* @return
*/
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
web.xml里面还有一些其他的配置:
我们在SpringMVC前面的学习中用到了两个过滤器,一个是编码过滤器,处理中文乱码问题;还有一个HiddenHttpMethodFilter过滤器,可以处理PUT和DELETE请求。在web.xml中配置如下:
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceResponseEncodingparam-name>
<param-value>trueparam-value>
init-param>
<init-param>
<param-name>forceRequestEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<filter>
<filter-name>HiddenHttpMethodFilterfilter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
用配置类也可以配置过滤器,只需要重写getServletFilters,注册对应的过滤器并返回就好了
/**
* 配置过滤器
* @return
*/
@Override
protected Filter[] getServletFilters() {
// 配置编码过滤器
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("utf-8");
characterEncodingFilter.setForceRequestEncoding(true);
characterEncodingFilter.setForceResponseEncoding(true);
// 配置处理请求方式为PUT和DELETE的HiddenHttpMethodFilter过滤器
HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
return new Filter[]{characterEncodingFilter,hiddenHttpMethodFilter};
}
springMVC配置文件里面有许多的配置,都可以通过这一个配置类来实现,具体配置有:
这里的组件扫描注解和spring里面的一样,在类上面加上@ComponentScan就可以,代表spring会将该类扫描到容器中。
等价于:
<context:component-scan base-package="com.cx">context:component-scan>
注解驱动配置是在类上面添加@EnableWebMvc注解。
等价于:
<mvc:annotation-driven>mvc:annotation-driven>
下面的方法要先实现WebMvcConfigurer接口
// 配置开启静态资源
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
等价于:
<mvc:default-servlet-handler>mvc:default-servlet-handler>
配置拦截器需要定义好拦截器,然后在注册,最后添加。我自己写了一个拦截器TestInterceptor 。addPathPatterns()方法是配置拦截路径,excludePathPatterns()方法是排除拦截路径。
// 配置拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
TestInterceptor testInterceptor = new TestInterceptor();
registry.addInterceptor(testInterceptor).addPathPatterns("/*");
registry.addInterceptor(testInterceptor).excludePathPatterns("/");
}
等价于:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<mvc:exclude-mapping path="/"/>
<ref bean="testInterceptor">ref>
mvc:interceptor>
mvc:interceptors>
// 配置ViewController视图解析器
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
等价于:
<mvc:view-controller path="/" view-name="index">mvc:view-controller>
// 配置文件上传解析器
@Bean
public MultipartResolver multipartResolver(){
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
return commonsMultipartResolver;
}
等价于:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">bean>
// 配置异常处理器
@Override
public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
Properties properties = new Properties();
properties.setProperty("java.lang.ArithmeticException","error");
simpleMappingExceptionResolver.setExceptionMappings(properties);
simpleMappingExceptionResolver.setExceptionAttribute("ex");
resolvers.add(simpleMappingExceptionResolver);
}
等价于:
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.ArithmeticException">errorprop>
props>
property>
<property name="exceptionAttribute" value="ex">property>
bean>