Spring完全基于Java配置和集成Junit单元测试
要点:
- 配置继承WebApplicationInitializer的类作为启动类,相当于配置web.xml文件
- 使用@Configuration注解一个类,在类中的方式使用@Bean注解,则表名该方法的返回值为一个Bean,相应于配置applicationContext.xml等spring的xml配置文件
配置启动类
继承WebApplicationInitializer并重新onStartup方法,加载配置类,相当于配置web.xml文件
1 public class WebAppInitConfig implements WebApplicationInitializer { 2 3 @Override 4 public void onStartup(ServletContext container) throws ServletException { 5 // Create the 'root' Spring application context 6 AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); 7 rootContext.register(RootConfig.class);//这是自定义的配置类,这个配置类会影响项目全局 8 9 // Manage the lifecycle of the root application context 10 container.addListener(new ContextLoaderListener(rootContext)); 11 12 // Create the dispatcher servlet's Spring application context 13 AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); 14 dispatcherContext.register(WebMvcConfig.class);//这是自定义的配置类,这个配置类只会影响当前模块 15 16 // Register and map the dispatcher servlet 17 ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); 18 dispatcher.setLoadOnStartup(1); 19 dispatcher.addMapping("/"); 20 } 21 }
AnnotationConfigApplicationContext:用来把使用注解的配置类加载到spring容器中,也就是把那些在配置类中定义的bean交给spring管理
也可以使用WebApplicationInitializer的实现类AbstractDispatcherServletInitializer、
AbstractAnnotationConfigDispatcherServletInitializer来进行更为简洁的配置,官方文档地址,只要实现了WebApplicationInitializer将会被servlet容器自动识别并加载
使用AbstractAnnotationConfigDispatcherServletInitializer配置示例如下
1 public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 2 3 @Override 4 protected Class<?>[] getRootConfigClasses() { 5 return new Class[]{RootConfig.class}; 6 } 7 8 @Override 9 protected Class<?>[] getServletConfigClasses() { 10 return new Class[]{WebMvcConfig.class};; 11 } 12 13 @Override 14 protected String[] getServletMappings() { 15 return new String[]{"/"}; 16 } 17 18 }
自定义配置类
配置spring管理bean,相应于配置applicationContext.xml等spring的xml配置文件,就是上面使用WebApplicationInitializer加载的类
1 @Configuration 2 @EnableWebMvc //注解驱动springMVC 相当于xml中的<mvc:annotation-driven> 3 @ComponentScan(basePackages = "com.woncode") //启用组件扫描 4 public class WebMvcConfig extends WebMvcConfigurerAdapter { 5 //配置静态资源的处理,要求DispatchServlet对静态资源的请求转发到servlet容器中默认的Servlet上 6 @Override 7 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 8 configurer.enable(); 9 } 10 11 @Override 12 public void addResourceHandlers(ResourceHandlerRegistry registry) { 13 registry.addResourceHandler("/statics/**").addResourceLocations("/WEB-INF/classes/statics/"); 14 super.addResourceHandlers(registry); 15 } 16 17 @Bean 18 public InternalResourceViewResolver viewResolver(){ 19 InternalResourceViewResolver resolver= new InternalResourceViewResolver(); 20 resolver.setPrefix("/WEB-INF/classes/templates/"); 21 resolver.setSuffix(".html"); 22 resolver.setExposeContextBeansAsAttributes(true); 23 return resolver; 24 } 25 26 }
@Import注解:用来引入其他配置类到当前配置类,然后就可以在当前配置类中使用其他配置类中的bean了,这在分模块的项目中很有用,因为分模块就是希望分割配置,但是有时又需要使用一些其他已经定义过的配置类,所以可以用此导入
集成JUnit单元测试
添加sprig-test的maven依赖
在测试类头上加如下注解,其中配置类HibernateConfig参考另一篇博文《基于Java配置Spring加Hibernate和再加SpringData时的差别》,是配置使用Spring+hibernate的
1 @Transactional 2 @WebAppConfiguration 3 @RunWith(SpringJUnit4ClassRunner.class) 4 @ContextConfiguration(classes = {WebMvcConfig.class, HibernateConfig.class}) 5 public class SpringTest { 6 }
注意:
- 如果使用Spring MVC则一定要加@WebAppConfiguration注解,否则无法载入web上下文环境,我在这个坑好久
- 在测试方法中如果进行一些数据库事物操作,比如要测试插入数据的方法,因为Spring测试套件在测试事务方法执行完成后会自动回滚,如果想要插入的数据持久保存到数据库中,则要在测试方法头上加@Rollback(false)注解