要點:
- 配置繼承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)注解