标签:配置文件 002SpringIOC005 配置 DemoConfiguration Bean 开发 注解 public
部分内容来自以下博客:
https://www.cnblogs.com/duanxz/p/7493276.html
1 概述
在使用Spring时,有两种方式管理Spring容器中的Bean,一种是通过XML文件进行管理,一种是通过注解进行管理。
为了能完全通过注解进行开发,Spring也提供了用于取代XML配置文件的注解。
2 代替配置文件
2.1 要求
使用Configuration注解可以将一个类作为配置类,替代XML配置文件,配置类需要满足以下要求:
不能是被final修饰的类。
不能是匿名类。
2.2 创建配置类
使用Configuration注解修饰配置类:
1 @Configuration 2 public class DemoConfiguration { 3 public DemoConfiguration() { 4 System.out.println("DemoConfiguration DemoConfiguration() ..."); 5 } 6 }
2.3 加载配置类
不再使用之前通过加载配置文件的方式获取容器,而是通过加载配置类的方式获取容器。
在测试类的方法中获取Spring容器:
1 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoConfiguration.class);
这一步相当于之前的代码:
1 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
还可以在配置类中引入其他配置类:
1 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); 2 context.register(DemoConfiguration.class); 3 context.refresh();
3 管理Bean
3.1 注册Bean
3.1.1 说明
使用Bean注解管理Spring容器中的Bean,类似于在配置文件中使用bean标签。
Bean注解中的属性有:
通过name属性指定组件名称,如果缺省则以当前方法名为组件名称。
通过initMethod属性指定组件加载时的方法。
通过initDestroy属性指定组件销毁时的方法。
3.1.2 使用
将Bean注解标注在配置类的方法上,该方法返回某个Bean的实例:
1 @Bean(name="demo", initMethod="init", destroyMethod="destroy", autowire=Autowire.BY_NAME) 2 public Demo getDemo() { 3 System.out.println("DemoConfiguration getDemo() ..."); 4 return new Demo(); 5 }
加载容器的时候会自动调用被Bean注解修饰的方法。
在执行完Bean的构造方法后还会执行指定的初始化方法,在销毁容器前也会执行指定的销毁方法。
3.2 扫描Bean
3.2.1 说明
使用ComponentScan注解扫描Bean,类似于在配置文件中使用component-scan扫描器。
ComponentScan注解的属性有:
basePackages:设置扫描类的路径。
basePackageClasses:设置扫描类的类名。
includeFilters:设置包含的过滤器。
excludeFilters:设置排除的过滤器。
3.2.2 标识
在类上使用注解将类作为一个Bean:
1 @Component 2 public class Demo { 3 public Demo() { 4 System.out.println("Demo Demo() ..."); 5 } 6 public void init() { 7 System.out.println("Demo init() ..."); 8 } 9 public void destroy() { 10 System.out.println("Demo destroy() ..."); 11 } 12 }
3.2.3 使用
在配置类上使用ComponentScan注解并设置扫描的属性:
1 @Configuration 2 @ComponentScan(basePackages="com.demo.bean") 3 public class DemoConfiguration { 4 public DemoConfiguration() { 5 System.out.println("DemoConfiguration DemoConfiguration() ..."); 6 } 7 }
4 配置Web应用程序
如果要在Web应用程序中使用程序式的配置,需要在web.xml中将contextClass这个属性值设为AnnotationConfigWebApplicationContext类,如果不指定,系统将加载默认的WebApplicationContext类来构建容器。
此外,使用WebApplicationContext类时需要使用contextConfigLocation指定配置文件,当使用AnnotationConfigWebApplicationContext类时,需要指定配置类。
添加并修改:
1 <context-param> 2 <param-name>contextClass</param-name> 3 <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 4 </context-param> 5 <context-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>com.demo.config.DemoConfiguration</param-value> 8 </context-param> 9 <listener> 10 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 11 </listener> 12 <servlet> 13 <servlet-name>springDispatcherServlet</servlet-name> 14 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 15 <init-param> 16 <param-name>contextConfigLocation</param-name> 17 <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 18 </init-param> 19 <load-on-startup>1</load-on-startup> 20 </servlet> 21 <servlet-mapping> 22 <servlet-name>springDispatcherServlet</servlet-name> 23 <url-pattern>/</url-pattern> 24 </servlet-mapping>
5 引入其他配置
5.1 引入XML配置文件
使用ImportResource注解引入其他XML配置:
1 @Configuration 2 @ImportResource("classpath:spring.xml") 3 public class DemoConfiguration { 4 5 public DemoConfiguration() { 6 System.out.println("DemoConfiguration DemoConfiguration() ..."); 7 } 8 }
5.2 引入配置类
使用注解引入其他配置类:
1 @Configuration 2 @Import(TestConfiguration.class) 3 public class DemoConfiguration { 4 5 public DemoConfiguration() { 6 System.out.println("DemoConfiguration DemoConfiguration() ..."); 7 } 8 }
6 EnableXxx注解
6.1 EnableAsync注解
在类上使用EnableAsync注解可以开启程序中的异步多线程功能,相当于XML配置文件中task名称空间的annotation-driven标签。
6.2 EnableScheduling注解
在类上使用EnableScheduling注解可以开启程序中的任务调度功能,相当于XML配置文件中task名称空间的annotation-driven标签。
标签:配置文件,002SpringIOC005,配置,DemoConfiguration,Bean,开发,注解,public 来源: https://www.cnblogs.com/zhibiweilai/p/15838257.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。