Spring3之 bean AutoWi

发布时间:2019-07-05 09:54:43编辑:auto阅读(1800)

    Autowiring collaborators 自动装配
    Spring通过检查BeanFactory中的内容,来替指定其他被依赖的bean
    优点:
    1显著减少配置的数量
    2以使配置与java代码同步更新
    XML配置过程中可<bean>标签中指定autowire属性,它有5个值(3中官方英文文档中只有前4个):
    no :No autowiringbean之间的关系必须通过ref标签来指定
    byName :根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。
    byType :如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置dependency-check="objects"Spring抛出异常。
    constructor :byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。
    @deprecated,autowire="autodetect"这个在spring2.5文档中还有见,但是在3中已经没了,3中的XML配置中添加些值会报错。
    autodetect :通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式。
    但是在spring3xsd文件中找到default这个值项,应该是替代autodetect 的吧:
    spring3.0Xsd 文件中
    default-autowire的属性只有no byNamebyTypebyType 这里既没有autodetect 也没有default
    default-autowire-candidates 中值的设置解释:
    A default bean name pattern for identifying autowire candidates:
    e.g. "*Service", "data*", "*Service*", "data*Service".
    Also accepts a comma-separated list of patterns: e.g. "*Service,*Dao".
    See the documentation for the 'autowire-candidate' attribute of the
    '<bean/>' element for the semantic details of autowire candidate beans.
    但是,我测试了下,发现并没有作用?搜到一外文网页上说如同上写法,但是那是07年的帖子了。是不是spring3中这个配置没了作用?
    /*
    dependency-check依赖检查自动装配中:autowire-candidate属性设为false,这样容器在查找自动装配对象时将不考虑该bean
    (这块在spring3中已经移除了?文档中没有找到
    依赖检查也可以针对每一个bean进行设置。依赖检查默认为not,它有几种不同的使用模式,在xml配置文件中,可以在bean定义中为dependency-check属性使用以下几种值:
    none: 没有依赖检查,如果bean的属性没有值的话可以不用设置。
    simple: 对于原始类型及集合(除其他被依赖的bean外的一切东西)执行依赖检查
    object: 仅对其他被依赖的bean执行依赖检查
    all: 其他被依赖的bean,原始类型及集合执行依赖检查
    也找不到:default-dependency-check
    */
    bean排除在自动装配之外
    一、当采用XML格式配置bean时,<bean/>元素的 autowire-candidate属性可被设为false,这样容器在查找自动装配对象时将不考虑该bean
    、使用对bean名字进行模式匹配来对自动装配进行限制。其做法是在<beans/>元素的'default-autowire-candidates'属性中进行设置。比如,将自动装配限制在名字以'Repository'结尾的bean,那么可以设置为"*Repository“。对于多个匹配模式则可以使用逗号进行分隔。注意,如果在bean定义中的'autowire-candidate'属性显式的设置为'true' 'false',那么该容器在自动装配的时候优先采用该属性的设置,而模式匹配将不起作用。
    < !--EndFragment-->
    测试代码:
    com.spring305.test.autowire.po.IdCard.java
    Java代码 复制代码 收藏代码
    1. public class IdCard {
    2. private String cardNo;
    3. private String desc;
    4. 。。。getter .setter
    5. }
    com.spring305.test.autowire.po.People.java
    Java代码 复制代码 收藏代码
    1. public class People {
    2. private int id;
    3. private String name;
    4. private IdCard idCard;
    5. public People(){
    6. }
    7. //autowire="constructor" 要有此构造方法
    8. public People(IdCard idCard){
    9. this.idCard = idCard;
    10. }
    11. getter setter
    12. }
    src/testAutoWire.xml
    Xml代码 复制代码 收藏代码
    1. <beans.... http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
    2. " default-autowire-candidates="*idCard" >
    3. <!-- default-autowire-candidates="*ple*,*ard*" 配置了,但是没有感觉到作用,并没有限制相应的 -->
    4. <!--beans标签中加上: default-autowire="default" or default-autowire="byName" (后面的是延迟加载,就不是了)default-lazy-init="true" -->
    5. <bean id="idCard" class="com.spring305.test.autowire.po.IdCard"></bean>
    6. <bean id="people" class="com.spring305.test.autowire.po.People" autowire="byName"></bean>
    7. <!--
    8. dependency-check="object" 这个在spring3中貌似没了
    9. 单个autowire
    10. <bean id="people" class="com.spring305.test.autowire.po.People" autowire="byName"></bean>
    11. <bean id="people" class="com.spring305.test.autowire.po.People" autowire="constructor"></bean>
    12. autowire-candidate="false" 不会自动注入到其它的bean中
    13. <bean id="idCard" class="com.spring305.test.autowire.po.IdCard" autowire-candidate="false"></bean>
    14. -->
    测试:
    Java代码 复制代码 收藏代码
    1. //@Test//xml
    2. public void AutoWireTestXML() {
    3. ApplicationContext context = new ClassPathXmlApplicationContext("testAutoWire.xml");
    4. People people = (People) context.getBean("people");
    5. IdCard idCard = people.getIdCard();
    6. // autowire="byName" 当people bean上没有加这个标签时,打印值为null,加后则打印出了对象
    7. System.out.println(idCard);
    8. }
    annotation测试:
    com.spring305.test.autowire.po.IdCardAnnotation.java
    Java代码 复制代码 收藏代码
    1. @Configuration
    2. public class IdCardAnnotation {
    3. private String cardNo;
    4. private String desc;
    5. 。。。。getter...setter
    6. }
    com.spring305.test.autowire.po.PeopleAnnotation.java
    Java代码 复制代码 收藏代码
    1. @Configuration
    2. public class PeopleAnnotation {
    3. private int id;
    4. private String name;
    5. private IdCardAnnotation idCardAnnotation;
    6. @Autowired(required = false)
    7. //@Qualifier("idCardAnnotation")
    8. public void setIdCardAnnotation(@Qualifier("idCardAnnotation")IdCardAnnotation idCardAnnotation) {
    9. this.idCardAnnotation = idCardAnnotation;
    10. }
    11. getter.setter
    12. }
    测试:
    Java代码
    1. @Test
    2. public void AutoWireTestAnnotation() {
    3. ApplicationContext context = new AnnotationConfigApplicationContext(IdCardAnnotation.class,PeopleAnnotation.class);
    4. PeopleAnnotation people = (PeopleAnnotation) context.getBean("peopleAnnotation");
    5. IdCardAnnotation idCard = people.getIdCardAnnotation();
    6. System.out.println(idCard);
    7. }

关键字