基于JavaConfig配置的Spring MVC的构建
上一篇讲了基于XML配置的构建,这一篇讲一讲基于JavaConfig的构建。为什么要写这篇文章,因为基于xml配置的构建,本人认为很麻烦,要写一堆的配置,不够简洁,而基于JavacConfig配置的构建符合程序员的编码习惯,是SpringMVC以后的趋势。不是说基于XML配置的不好,毕竟现在很多已有的项目都是基于XML的,只是基于JavaConfig配置的更优化,而且两种方式可以鱼和熊掌兼得。但要知道的是,基于JavaConfig配置构建的Spring MVC项目只能部署到支持Servlet 3.0服务器上面。
进入正题,开始搭建项目:
搭建环境:
1. IDEA
2. Maven
3. Tomcat
搭建步骤:
1. 创建maven-archetype-webapp项目,这个项目是maven的web项目
2.在pom中添加sping mvc相关的依赖包,这个依赖包是我自己使用的,包含了单元测试和相关的mvc mock包:
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.9.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/javax.servlet.jsp.jstl-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.10.1.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.8.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>2.8.47</version> </dependency> </dependencies>
3.将DispatcherSevlet配置在Servlet容器中
public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { /*加载后端的中间层和数据层组件*/ @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[]{RootConfig.class}; } /*加载web组件,控制器、试图解析器、处理器映射*/ @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[]{WebConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } }
示例代码中要知道的是,任何扩展了AbstractAnnotationConfigDispatcherServletInitializer的类都会自动配置DispatcherServlet和Spring应用上下文。实际上,AbstractAnnotationConfigDispatcherServletInitializer会同时创建DispatcherServlet和ContextLoaderListener。getServletConfigClasses()方法返回的带有@Configuration注解的类会用来定义DispatcherServlet应用上下文中的bean。getRootConfigClasses()方法返回的带有@Configuration注解的类会用来定义ContextLoaderListener应用上下文中的bean。
4.创建WebConfig类
@Configuration @EnableWebMvc @ComponentScan("spittr.web") public class WebConfig extends WebMvcConfigurerAdapter { /*配置视图解析器*/ @Bean public ViewResolver viewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); resolver.setExposeContextBeansAsAttributes(true); return resolver; } /*配置静态资源的处理*/ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } }
5.创建RootConfig类
@Configuration @ComponentScan(basePackages = {"spittr"},excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class)}) public class RootConfig { }
6,到这里配置已经结束。怎样创建Controller和运行请参考上一篇基于XML配置的Spring MVC。
7.最后附上源码:源码