Spring完全基於Java和註解配置

來源:互聯網
上載者:User

標籤:patch   資源   art   類載入   bst   自動識別   void   javadoc   ica   

要點:

  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 context10         container.addListener(new ContextLoaderListener(rootContext));11 12         // Create the dispatcher servlet‘s Spring application context13         AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();14         dispatcherContext.register(WebMvcConfig.class);//這是自訂的配置類,這個配置類只會影響當前模組15 16         // Register and map the dispatcher servlet17         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     @Override14     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     @Override12     public void addResourceHandlers(ResourceHandlerRegistry registry) {13         registry.addResourceHandler("/statics/**").addResourceLocations("/WEB-INF/classes/statics/");14         super.addResourceHandlers(registry);15     }16 17     @Bean18     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和註解配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.