1. 程式人生 > >Spring完全基於Java和註解配置

Spring完全基於Java和註解配置

進行 要點 servle containe one targe 引入 logs pos

要點:

  1. 配置繼承WebApplicationInitializer的類作為啟動類,相當於配置web.xml文件
  2. 使用@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的實現類AbstractDispatcherServletInitializerAbstractAnnotationConfigDispatcherServletInitializer來進行更為簡潔的配置,官方文檔地址,只要實現了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了,這在分模塊的項目中很有用,因為分模塊就是希望分割配置,但是有時又需要使用一些其他已經定義過的配置類,所以可以用此導入

Spring完全基於Java和註解配置