码迷,mamicode.com
首页 > 编程语言 > 详细

Spring完全基于Java和注解配置

时间:2017-09-05 23:13:48      阅读:248      评论:0      收藏:0      [点我收藏+]

标签: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 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和注解配置

标签:patch   资源   art   类加载   bst   自动识别   void   javadoc   ica   

原文地址:http://www.cnblogs.com/woncode/p/7476285.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!