Spring4 Bean管理的注解实现
来源:互联网 发布:比较好的网红的淘宝店 编辑:程序博客网 时间:2024/06/04 18:27
日期: 2016-7-16
内容: 学习Spring Bean管理的注解实现;
主要内容:
1、ClassPath扫描与组件管理;
2、类的自动检测与注册Bean;
Spring可以自动检测类并注册Bean到ApplicationContext中
@Servicepublic class SimpleMovieLister{ private MovieFinder movieFinder; @Autowired publicc SimpleMovieLister(MovieFinder movieFinder){ this.movieFinder = movieFinder;}}
3、<context:annotation-config/>;
通过在基于XML的Spring陪住如下标签(请添加上下文的命名空间);
<context:annotation-config/>仅会查找在同一个applicationContext中的bean注解。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 添加Spring类注解标签 --> <context:annotation-config></context:annotation-config> <bean id="injectionService" class="com.test.service.InjectionServiceImpl"> <property name="injectionDAO" ref="injectionDAO"></property> </bean> <bean id="injectionDAO" class="com.test.dao.InjectionDAOImpl"></bean> </beans>为了
让Spring能够检测到这些类并添加注册相应的Bean需要添加如下的内容
<!-- 为了让Spring能够检测并注册相应的Bean需要添加如下的内容 --> <context:component-scan base-package="org.example.hello(添加包,也可以使用正则表达式)"></context:component-scan>
但是在通常的情况下:<context:component-scan base-package="org.example.hello(添加包,也可以使用正则表达式)"></context:component-scan>
包含了<context:annotation-config></context:annotation-config>,所以通常在使用了前者时候就不再使用后者了。AutowiredAnnotationbeanPostProcessor和CommonAnnotationbeanPostProcessor也会被包含进来。
其实意思是这样的,在applicationContext的配置文件中添加了:<context:component-scan base-package="org.example.hello(添加包,也可以使用正则表达式)">配置
之后,在启动Spring IOC容器的时候,就会自动去扫描指定的包上的注解,在扫描到符合Bean配置的类的时候就会将其添加在Spring
的配置文件中并将其注册(实例化)。
使用过滤器进行自定义扫描:
默认情况下,类被自动发现并注册bean的条件是: 使用@Component,@RePository,@Service,@Controller注解或者使用@Component的自定义注解
可以通过过滤器修改上面的行为,如:下面的例子的XML配置忽略所有的有@Repository注解并用“”Stub“”代替。
<!-- 为了让Spring能够检测并注册相应的Bean需要添加如下的内容 --> <context:component-scan base-package="org.example.hello(添加包,也可以使用正则表达式)"> <!-- 使用过滤器,过滤掉一些不需要注册Bean的类 --> <context:exclude-filter type="regex" expression=""/> 使用正则表达式除去不需要的类 --> <!--<context:include-filter type="regex" expression=""/> </context:component-scan>
其中过滤的方式有:
@Service(""helloLUOt)public class Test1{ //add somethings}@Repository("helloWOrld"){ //add somethings}
package com.test.implBeanNameGenerator;import org.springframework.beans.factory.config.BeanDefinition;import org.springframework.beans.factory.support.BeanDefinitionRegistry;import org.springframework.beans.factory.support.BeanNameGenerator;public class GeneratorBeanName implements BeanNameGenerator {@Overridepublic String generateBeanName(BeanDefinition arg0,BeanDefinitionRegistry arg1) {return null;}}
<!-- 添加beanName的自定义 --> <context:component-scan base-package="com.test.implBeanNameGenerator" name-generator="com.test.implBeanNameGenerator.GeneratorBeanName"> </context:component-scan>
@Repository@Scope("prototype")public class HelloWorld{ //do sometingh}
也可以自定义scope策略,实现ScopeMetadataResolver接口并提供一个无参数的构造器。
代理方式:
可以使用scoped-proxy属性指定代理,有三个值可选:no,interfaces,targetClass.
<!-- 使用interfaces代理 --> <context:component-scan base-package="org.demo" scoped-proxy="interfaces"></context:component-scan> <!-- 不使用代理:no --> <context:component-scan base-package="org.demo" scoped-proxy="no"></context:component-scan> <!-- 使用targetClass方式代理 --> <context:component-scan base-package="org.demo" scoped-proxy="targetClass"></context:component-scan>
package com.test.annotation;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;@Service("bean")@Component(""beanname)//这个注解不OKpublic class TestBeanAnnotation {public void say(String word){System.out.println("BeanAnnotation: "+word);}}
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 实现bean的扫描和注册 --> <context:component-scan base-package="com.test.annotation"></context:component-scan> </beans>
@Testpublic void testSay(){//加载配置文件ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_BeanAnnotation.xml");//获得实例TestBeanAnnotation ta = (TestBeanAnnotation)ac.getBean("bean");ta.say("hello world!");}
开始执行: 2016年07月16日 03时:34分:36秒七月 16, 2016 3:34:36 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7960847b: startup date [Sat Jul 16 15:34:36 CST 2016]; root of context hierarchy七月 16, 2016 3:34:37 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext_BeanAnnotation.xml]BeanAnnotation: hello world!执行完成: 2016年07月16日 03时:34分:36秒
4、@Component,@Repository,@Service,@Controller
在类中添加这几个注解之后,当Spring IOC容器启动的时候就会去检查并将这些类实例化为Bean提供调用。
5、@Required;
这个注解适用于bean属性的setter方法。这个注解仅仅表示,受影响的bean属性必须在配置时被填充,通过在bean定义或自动装配一个明确的属性值。
6、@Autowired;
可以将这个注解理解为传统的setter方法,可以用于构造器或者成员变量。默认情况下,如果找不到合适的bean将会导致autowiring失败抛出异常,可以通过下面的方式避免。
在使用类的时候,每个类可能有很多个构造器,但是在使用Autowored的时候每一个类只能有一个构造器被标记为required=true,@Autowired的必要属性,建议使用@Required注解。
测试例子:第一种注入方式,在给属性添加@Autowired注解的情况下。
package com.test.dao;public interface InjectionDAO {public void save(String arg);}
package com.test.dao;import org.springframework.stereotype.Repository;@Repositorypublic class InjectionDAOImpl implements InjectionDAO {@Overridepublic void save(String arg) {System.out.println("保存的数据: "+arg);}}
package com.test.service;public interface InjectionService {public void save(String arg);}
package com.test.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.test.dao.InjectionDAO;@Servicepublic class InjectionServiceImpl implements InjectionService {@AutowiredInjectionDAO injectionDAO;@Overridepublic void save(String arg) {System.out.println("接收的数据: "+arg);//处理数据arg = arg+":"+this.hashCode();//调用InjectionDAO的save方法处理数据injectionDAO.save(arg);//--->配置xml配置文件}}
XML配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 配置注解自动扫描 Bean--> <context:component-scan base-package="com.test"></context:component-scan> </beans>
测试方法:
@Testpublic void testAutowired(){//加载配置文件ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//获得实例InjectionService is = (InjectionService)ac.getBean("injectionServiceImpl");//调用保存方法保存数据is.save("@Autowired");}
测试结果:
开始执行: 2016年07月16日 08时:27分:54秒七月 16, 2016 8:27:55 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7960847b: startup date [Sat Jul 16 20:27:55 CST 2016]; root of context hierarchy七月 16, 2016 8:27:55 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext.xml]接收的数据: @Autowired保存的数据: @Autowired:1848415041执行完成: 2016年07月16日 08时:27分:54秒
测试例子:第二种注入方式,在给setter方法添加@Autowired注解的情况下。
package com.test.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.test.dao.InjectionDAO;@Servicepublic class InjectionServiceImpl implements InjectionService {//@Autowired: 给属性添加注解InjectionDAO injectionDAO;@Autowired//给setter方法添加注解public void setInjectionDAO(InjectionDAO injectionDAO) {this.injectionDAO = injectionDAO;}@Overridepublic void save(String arg) {System.out.println("接收的数据: "+arg);//处理数据arg = arg+":"+this.hashCode();//调用InjectionDAO的save方法处理数据injectionDAO.save(arg);//--->配置xml配置文件}}
运行结果:
开始执行: 2016年07月16日 08时:35分:35秒七月 16, 2016 8:35:35 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7960847b: startup date [Sat Jul 16 20:35:35 CST 2016]; root of context hierarchy七月 16, 2016 8:35:35 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext.xml]接收的数据: @Autowired保存的数据: @Autowired:518522822执行完成: 2016年07月16日 08时:35分:35秒
得到的结果是一样的。
测试例子:第三种注入方式,在构造器上使用@Autowired注解的方式。
package com.test.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.test.dao.InjectionDAO;@Servicepublic class InjectionServiceImpl implements InjectionService {//@Autowired: 给属性添加注解InjectionDAO injectionDAO;//@Autowired//给setter方法添加注解public void setInjectionDAO(InjectionDAO injectionDAO) {this.injectionDAO = injectionDAO;}@Autowired//使用构造器添加注解注入public InjectionServiceImpl(InjectionDAO injectionDAO){this.injectionDAO = injectionDAO;}@Overridepublic void save(String arg) {System.out.println("接收的数据: "+arg);//处理数据arg = arg+":"+this.hashCode();//调用InjectionDAO的save方法处理数据injectionDAO.save(arg);//--->配置xml配置文件}}
运行结果:
开始执行: 2016年07月16日 09时:10分:00秒七月 16, 2016 9:10:00 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7960847b: startup date [Sat Jul 16 21:10:00 CST 2016]; root of context hierarchy七月 16, 2016 9:10:00 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext.xml]接收的数据: @Autowired保存的数据: @Autowired:1738236591执行完成: 2016年07月16日 09时:10分:00秒
总结: 使用@Autowired可以完成成员变量注入,setter注入和构造器注入三种方式的注入,最后的运行结果都是相同的。
第二部分:
可以使用@Autowired来注解那些众所周知的解析依赖性接口,比如: BeanFactory,ApplicationContext,Environment,ResourLoader,ApplicationEvent,和MessageSource。
如果希望数组有序,可以让bean实现org.springframework.core.Ordered接口或者使用@Order注解。
@Autowired注解是由Spring的BeanPostProcessor处理的所以不能再自己的BeanPostProcessor类型运用这些注解,这些类型必须通过XML或者@Bean注解加载。
例子:数组及Map的自动注入:
package com.test.multibean;public interface BeanInterface {}
package com.test.multibean;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Order(value=1)@Componentpublic class BeanImplOne implements BeanInterface {}
package com.test.multibean;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Order(value=2)@Componentpublic class BeanImplTwo implements BeanInterface {}
package com.test.multibean;import java.util.List;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;//调用类@Componentpublic class BeanInvoker {//List的方式@Autowiredprivate List<BeanInterface> list;//Map的方式@Autowiredprivate Map<String,BeanInterface> map;public void say(){System.out.println("List方式: ");if(null != list &&list.size() != 0){for (BeanInterface bean : list) {System.out.println(bean.getClass().getName());}}else{System.out.println("List<BeanInterface> is null!!!!");}System.out.println("Map方式: ");if(null != map &&map.size() !=0){//遍历mapfor(Map.Entry<String, BeanInterface> entry:map.entrySet()){System.out.println(entry.getKey()+" "+entry.getValue().getClass().getName());}}else{System.out.println("Map<String,BeanInterface> map is null!");}}}
测试类:
@Testpublic void testBeanInvoke(){//加载配置文件ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//获得实例BeanInvoker bi = (BeanInvoker)ac.getBean("beanInvoker");bi.say();}
测试结果:
开始执行: 2016年07月16日 09时:58分:50秒七月 16, 2016 9:58:51 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7960847b: startup date [Sat Jul 16 21:58:51 CST 2016]; root of context hierarchy七月 16, 2016 9:58:51 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext.xml]List方式: com.test.multibean.BeanImplOnecom.test.multibean.BeanImplTwoMap方式: beanImplOne com.test.multibean.BeanImplOnebeanImplTwo com.test.multibean.BeanImplTwo执行完成: 2016年07月16日 09时:58分:50秒
7、@Aualifier;
8、@Resource;
从Spring3.0开始,Spring JavaConfig项目提供了很多特性,包括使用Java而不是XML配置文件定义Bean。比如:
@Component,@Bean,@Import,@DependsOn.其中@Component是一个通用注解,可以使用于任何Bean。
@Repository,@Service,@Controller是更有针对性的注解
--@Repository通常用于注解DAO类,即持久层;
--@Service通常用于注解Service类,即服务层;
--@Controller通常用于Controller类,即控制层(MVC);
元注解: 许多Spring提供的注解可以作为自己的代码,即“元数据注解”,元注解是一个简单的注解,可以应用到了一个注解
除了value(),元注解还可以有其他的属性,允许定制。
- Spring4 Bean管理的注解实现
- Spring4----使用注解方式配置bean和bean的属性
- Spring的bean管理(注解)
- Spring4-@Enable** 注解的实现原理
- Spring4学习笔记-通过注解配置bean
- spring4+mybatis 配置注解装载bean
- Spring4学习(三):注解配置bean
- Spring4--2.bean注解和AOP
- Spring的bean管理(注解)
- Spring的bean管理(注解方式)
- 基于注解的方式管理Bean
- Spring的bean管理-注解创建对象
- Spring入门——Bean管理的注解实现及例子
- Spring4的知识应用总结(四)——Bean的生命周期和注解方式配置
- Spring4.0学习笔记006——Bean的配置(基于注解)
- Spring4学习笔记(八):通过注解的方式配置bean和@Autowired 自动装配
- 基于Spring4的全注解实现Restful开发
- 基于Spring4的全注解实现Restful开发
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- php的empty函数参数不能是一个函数
- 修改CentOs7 上Docker默认文件系统DeviceMapper为OverlayFS
- 函数参数前是否加&的问题
- replicate_do_db和repalicate_wild_do_table跨库同步问题
- Spring4 Bean管理的注解实现
- Mob的ShareSDK
- php递归函数相关知识
- 将外部数据文件导入到hive中
- RxBinding的使用,实现数据和View的绑定
- 303. Range Sum Query - Immutable
- 日志处理方法及系统
- mysql 主从复制配置
- !$a和isset($a)和empty($a)的区别