Spring2.5学习笔记1-控制反转-Autowiring策略(续)

三、@Autowired注解
     @Autowired注解作用与属性 方法和构造器,启用此注解需要定义o rg.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor
required=false 可以使的@ Autowired注解的属性方法在找不到协作者时不报错
 小结:@Required注解针对dependency-check属性提出,@Autowired针对autowire属性提出,@Autowired的required 可以进行@Required的依赖检查工作
 四 、 细粒度Autowiring策略控制
          primary属性虽然可以避免多个协作者的异常,但是不能满足企业级应用需求,比如多个同类型不同协作者注入到它能够以受管bean的时候。@Qualifier注解可以解决此类问题。

package cn.beanfactory.bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class QualifierBean {
  @Autowired
  @Qualifier( "h1") //默认注解
  @FineQualifier(keyFine= "k1",valueFine= "v1") //自定义注解
     private HelloWorld hw1;
  @Autowired
  @Qualifier( "h2") //默认注解
  @FineQualifier(keyFine= "k2",valueFine= "v2") //自定义注解
     private HelloWorld hw2;
   public HelloWorld getHw2() {
     return hw2;
  }
   public void setHw2(@FineQualifier(keyFine= "k2",valueFine= "v2") HelloWorld hw2) {
     this.hw2 = hw2;
  }
   public HelloWorld getHw1() {
     return hw1;
  }
   public void setHw1(@FineQualifier(keyFine= "k1",valueFine= "v1") HelloWorld hw1) {
     this.hw1 = hw1;
  }

}

package cn.beanfactory.bean;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Qualifier;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.PARAMETER})
@Qualifier //无此注释时     需要借助 org.springframework.beans.factory.annotation.CustomAutowireConfigurer对象
public @ interface FineQualifier {
    String keyFine() default "";
    String valueFine() default "";

}

<!-- @Qualifier 注解 -->
     < bean name ="qualifierBean" class ="cn.beanfactory.bean.QualifierBean"         autowire ="byType" >
    
     </ bean >
     < bean name ="hw1" class ="cn.beanfactory.bean.HelloWorld" >
    <!-- 自定义限定注解 -->
     < qualifier type ="cn.beanfactory.bean.FineQualifier" >
         < attribute key ="keyFine" value ="k1" />
         < attribute key ="valueFine" value ="v1" />
     </ qualifier >
    <!--         <qualifier value="h1"/> -->
     </ bean >
     < bean name ="hw2" class ="cn.beanfactory.bean.HelloWorld" >
    <!-- 自定义限定注解 -->
     < qualifier type ="cn.beanfactory.bean.FineQualifier" >
         < attribute key ="keyFine" value ="k2" />
         < attribute key ="valueFine" value ="v3" />
     </ qualifier >
    <!-- <qualifier value="h2"/> -->
     </ bean >
  <!-- FineQualifier 无类一级的@Qualifier时需要借助以下对象 -->
  
   < bean id ="customAutowireConfiger"
     class ="org.springframework.beans.factory.annotation.CustomAutowireConfigurer" >
     < property name ="customQualifierTypes" >
       < set >
         < value >cn.beanfactory.bean.FineQualifier </ value >
       </ set >
     </ property >
   </ bean >

      注:<context:annotation-config /> 元素可以提供各种注解的支持对象例如: RequiredAnnotationBeanPostProcessor,但是却不能自定义 RequiredAnnotationBeanPostProcessor行为

五、depends-on 属性
ref属性虽然能完成依赖注入,但是不能保证按照开发者指定的循序实例化,depends-on 属性可以强制指定依赖对象,从而优先实例化依赖对象。
      
<!-- @Qualifier 注解 -->
   < bean name ="qualifierBean" class ="cn.beanfactory.bean.QualifierBean" depends-on ="hw1,hw2"
     autowire ="byType" >

   </ bean >
   < bean name ="hw1" class ="cn.beanfactory.bean.HelloWorld" >
    <!-- 自定义限定注解 -->
     < qualifier type ="cn.beanfactory.bean.FineQualifier" >
       < attribute key ="keyFine" value ="k1" />
       < attribute key ="valueFine" value ="v1" />
     </ qualifier >
    <!-- <qualifier value="h1"/> -->
   </ bean >
   < bean name ="hw2" class ="cn.beanfactory.bean.HelloWorld" >
    <!-- 自定义限定注解 -->
     < qualifier type ="cn.beanfactory.bean.FineQualifier" >
       < attribute key ="keyFine" value ="k2" />
       < attribute key ="valueFine" value ="v2" />
     </ qualifier >
    <!-- <qualifier value="h2"/> -->
   </ bean >

六、抽象Bean和子Bean

package cn.beanfactory.bean;

public class AbstractBean {
   private String username;
   private String password;
   public String getUsername() {
     return username;
  }
   public void setUsername(String username) {
     this.username = username;
  }
   public String getPassword() {
     return password;
  }
   public void setPassword(String password) {
     this.password = password;
  }
  
  

}

  log.info( "==========抽象类和子Bean==========");

    AbstractBean abstractBean = gbfa.getBean( "childBean1");
    log.info( "userName=" + abstractBean.getUsername());
    log.info( "password=" + abstractBean.getPassword());

    AbstractBean abstractBean2 = gbfa.getBean( "childBean2");
    log.info( "userName=" + abstractBean2.getUsername());
    log.info( "password=" + abstractBean2.getPassword());
    AbstractBean abstractBean3 = gbfa.getBean( "childBean3");
    log.info( "userName=" + abstractBean3.getUsername());
    log.info( "password=" + abstractBean3.getPassword());




  <!-- 抽象和子Bean -->
  <!-- 抽象Bean Spring 不会实例化 -->
   < bean id ="abstractBean1"     class ="cn.beanfactory.bean.AbstractBean" abstract ="true" >
     < property name ="username" value ="un_ab1" />
   </ bean >
   < bean id ="childBean1" class ="cn.beanfactory.bean.AbstractBean" parent ="abstractBean1" >
     < property name ="password" value ="pwd_cb1" />
   </ bean >
  
   < bean id ="abstractBean2" abstract ="true" >
     < property name ="username"     value ="un_ab2" />
   </ bean >
   < bean id ="childBean2" class ="cn.beanfactory.bean.AbstractBean" parent ="abstractBean2"     >
     < property name ="password" value ="pwd_cb2" />
   </ bean >
    <!-- 继承    -->
   < bean id ="childBean3" class ="cn.beanfactory.bean.AbstractBean" parent ="childBean2" >
     < property name ="username" value ="un_cb3" />    <!--  覆盖父Bean属性值   -->
   </ bean >

七、别名 alias
<alias name="testBean" alias="tb"/>



你可能感兴趣的:(注解,职场,策略,休闲,Autowiring)