1.1-Spring 依赖注入

构造函数的依赖注入

关键字:constructor-arg
bean.xml中使用constructor-arg指向某个定义的bean,可以直接传递给构造函数

public class Test1 {
   private Test2 test2;
   private int int1;
   private String str1;
   public Test1 (Test2 test2,int int1,String str1) {
      System.out.println("test1的构造函数" );
      this.int1= int1;
      this.str1 = str1;
      this.test2 = test2;
      }
   public void spellTest2() {
      test2.check();
   }
}

public class Test2 {
   public Test2 (){
      System.out.println("test2的构造函数" );
   }
   public void check() {
      System.out.println("调用了test2的check()方法." );
   } 
}

Bean.xml的配置
容器也可以使用与简单类型匹配的类型

<bean id="test1" class="com.xxx.xxx.Test1">
      <constructor-arg ref="testTwo"/>
      <constructor-arg type="int" value="2018"/>
      <constructor-arg type="java.lang.String" value="test"/>
</bean>

<bean id="testTwo" class="com.xxx.xxx.Test2">
</bean>

或者使用index关键字进行显式的配置,即配置参数的下标

<bean id="test1" class="com.xxx.xxx.Test1">
      <constructor-arg index="0" ref="testTwo"/>
      <constructor-arg index="1" value="2018"/>
      <constructor-arg index="2" value="test"/>
</bean>

基于设值函数的依赖注入

类中的属性设置了get/set方法后,就可以基于它来进行依赖注入

public class Test1 {
    private Test2 test2;
    private String name;
    public setTest2 (Test2 test2) {
      System.out.println("test2的设值函数" );
      this.test2 = test2;
    }
    public Test2 getTest2(){
        return test2;
    }
    public setName (String name) {
      System.out.println("name的设值函数" );
      this.name= name;
    }
    public String getName(){
        return name;
    }
}

public class Test2 {
   public Test2 (){
      System.out.println("test2的构造函数" );
   }
}

bean.xml的配置
通过关键字name指向类中的属性名,前提是这个属性名有set方法的设值函数

<bean id="textEditor" class="com.tutorialspoint.TextEditor">
    <property name="test2" ref="testTwo"/>
    <property name="name" value="testStr"/>
</bean>

<bean id="testTwo" class="com.xxx.xxx.Test2"/>

使用关键字p-namespace 进行设值函数的依赖注入

<bean id="test1" class="com.xxx.xxx.Test1"
      p:name="testStr"
      p:spouse-ref="testTwo"/>
</bean>

<bean id="testTwo" class="com.xxx.xxx.Test2"/>

Spring 注入内部 Beans

Java 内部类是在其他类的范围内被定义的,bean同理

```
public class Test1 {
   private Test2 test2;

   public Test1 (Test2 test2) {
      System.out.println("test1的构造函数" );
      this.test2 = test2;
      }
   public void spellTest2() {
      test2.check();
   }
}

public class Test2 {
   public Test2 (){
      System.out.println("test2的构造函数" );
   }
   public void check() {
      System.out.println("调用了test2的check()方法." );
   } 
}

bean.xml的配置

 <bean id="test1" class="com.xxx.xxx.Test1">
      <property name="test2">
         <bean id="testTwo" class="com.xxx.xxx.Test2"/>
       </property>
   </bean>

Spring 注入集合

元素 描述
list 它有助于连线,如注入一列值,允许重复。
set 它有助于连线一组值,但不能重复。
map 它可以用来注入名称-值对的集合,其中名称和值可以是任何类型。
props 它可以用来注入名称-值对的集合,其中名称和值都是字符串类型。

定义一个有不同类型的get/set类

public class JavaCollection {
   List addressList;
   Set  addressSet;
   Map  addressMap;
   Properties addressProp;
   //省略get/set 方法
}

bean.xml的配置
集合内可以传入具体的值也可以传入引用

 <bean id="javaCollection" class="com.xxx.xxx.JavaCollection">

      <property name="addressList">
         <list>
            <value>test1</value>
            <value>test2</value>
            <ref bean="test1"/>
         </list>
      </property>

      <property name="addressSet">
         <set>
            <value>test1</value>
            <value>test2</value>
            <ref bean="test1"/>
        </set>
      </property>

      <property name="addressMap">
         <map>
            <entry key="1" value="test1"/>
            <entry key="2" value="test2"/>
            <entry key ="3" value-ref="test1"/>
         </map>
      </property>

      <property name="addressProp">
         <props>
            <prop key="one">test1</prop>
            <prop key="two">test2</prop>
            <prop key="three">test3</prop>
         </props>
      </property>
   </bean>
   <bean id="test1" class="com.xxx.xxx.Test1"/>
发布了113 篇原创文章 · 获赞 48 · 访问量 34万+

猜你喜欢

转载自blog.csdn.net/yehui928186846/article/details/81269872
今日推荐