使用java類代替web.xml配置(實訓項目開發)
使用spring-webmvc依賴包中的AbstractAnnotationConfigDispatcherServletInitializer抽象方法代替web項目中的xml配置。
AbstractAnnotationConfigDispatcherServletInitializer的任意類都會自動配置DispatcherServlet和Spring應用上下文,Spring應用上下文會位於應用程序的servlet上下文中。
使用Java類代替web.xml的配置非常簡單,完全可以套用模板,下面就是代碼
1.編寫總配置類
1 public class EpidemicApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 2 /** 3 * 用來配置ContextLoaderListener創建對應上下文的bean 4 * @return 5 */ 6 @Override 7 protected Class<?>[] getRootConfigClasses() { 8 //Spring相關配置 9 return new Class[]{SpringConfig.class}; 10 //花括號內填寫Spring配置的類名或者路徑,這里的名字是SpringConfig.class 11 } 12 13 /** 14 * 用於定義DispatcherServlet應用上下文的bean 15 * @return 16 */ 17 @Override 18 protected Class<?>[] getServletConfigClasses() { 19 //SpringMVC相關配 20 return new Class[]{SpringMVCConfig.class}; 21 //花括號內填寫SpringMvc配置的類名或者路徑,這里的名字是SpringMVCConfig.class 22 } 23 24 /** 25 * 配置攔截器 26 * @return 27 */ 28 @Override 29 protected String[] getServletMappings() { 30 //返回映射到DispatcherServlet(前端控制器)的請求路徑 31 return new String[]{"/"}; 32 //花括號內填寫需要攔截的頁面, 33 } 34 }
如果使用idea開發項目,在需要重寫方法時可以使用alt+insert鍵來快速重寫方法
2.編寫Spring配置類
使用@Configuration、@ComponentScan(組件掃描)和@EnableTransactionManagement等注解標簽配置
① @Configuration用於定義配置類,可替換xml配置文件,被注解的類內部包含有一個或多個被@Bean注解的方法,這些方法將會被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext類進行掃描,並用於構建bean定義,初始化Spring容器。
② @ComponentScan 注解默認會掃描該類所在的包下所有的配置類,相當於之前的<context:component-scan>
③ @EnableTransactionManagement 用於開啟事務支持
1 /** 2 * Spring相關配置(替換掉了spring-bean.xml和spring-tx.xml) 3 */ 4 @Configuration 5 @ComponentScan(excludeFilters = @ComponentScan.Filter(type= FilterType.ANNOTATION,classes = Controller.class)) 6 @EnableTransactionManagement //啟用事務管理器 7 public class SpringConfig { 8 @Bean 9 /* 10 *平台事務管理器,這里的方法名是transactionManager 11 */ 12 public PlatformTransactionManager transactionManager(DataSource dataSource){ 13 //創建數據源事務管理器,數據源可以在MyBatis里配置 14 DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(); 15 //DataSourceTransactionManager實現類 16 transactionManager.setDataSource(dataSource); 17 return transactionManager; 18 } 19 20 }
3.編寫SpringMvc配置類
@Autowired 注解可以對類成員變量、方法及構造函數進行標注,讓 spring 完成 bean 自動裝配的工作。
1 /** 2 * springmvc相關配置(替換掉了springmvc.xml) 3 */ 4 @Configuration 5 @EnableWebMvc 6 @ComponentScan(includeFilters = @ComponentScan.Filter(type= FilterType.ANNOTATION,classes = Controller.class)) 7 //繼承WebMvcConfigurationSupport類也可以,但是這個類需要重寫的方法比較多,所以這里繼承了WebMvcConfigurerAdapter類 8 public class SpringMVCConfig extends WebMvcConfigurerAdapter { 9 @Autowired 10 private DateConverter dateConverter; 11 /** 12 * 添加視圖控制器,如果沒有指定,就默認訪問這個界面 13 * @param registry 14 */ 15 @Override 16 public void addViewControllers(ViewControllerRegistry registry) { 17 registry.addViewController("/").setViewName("epidemic");//訪問的是epidemic.jsp,如果擴展名就需要配置視圖解析器來定義擴展名 18 } 19 20 /** 21 * 配置視圖解析器(前后綴) 22 * @param registry 23 */ 24 @Override 25 public void configureViewResolvers(ViewResolverRegistry registry) { 26 //定義了一個內部資源視圖解析器(InternalResourceViewResolvers) 27 registry.jsp("/",".jsp"); 28 } 29 30 /** 31 * 配置日期轉換器 32 * @param registry 33 */ 34 @Override 35 public void addFormatters(FormatterRegistry registry) { 36 registry.addConverter(dateConverter); 37 } 38 39 /** 40 * 如果沒有匹配到action.就使用servlet默認的訪問 41 * @param configurer 42 */ 43 @Override 44 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 45 //簡單來說,就是為給靜態資源放行 46 configurer.enable(); 47 } 48 }
