spring_2_注入详解

1. 注入(Injection)

1.1 什么是注入?

注入:通过 Spring ⼯⼚及配置⽂件,为所创建对象的成员变量赋值。

1.2 为什么要注入?

  • 通过编码的⽅式,为成员变量进⾏赋值,存在耦合。
  • 注入的好处:解耦合。
public void test4() {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
    Person person = (Person) ctx.getBean("person");
    // 通过代码为变量赋值, 存在耦合, 如果我们以后想修改变量的值, 需要修改代码, 重新编译
    person.setId(1);
    person.setName("zhenyu");
    System.out.println(person);
}

1.3 如何进行注入[开发步骤]

  • 类的成员变量提供 set get ⽅法
  • 配置 spring 的配置⽂件
<bean id="person" name="p" class="com.yusael.basic.Person">
  <property name="id">
    <value>10</value>
  </property>
  <property name="name">
    <value>yusael</value>
  </property>
</bean>

1.4 Spring注入的原理分析(简易版)

Spring 底层通过 调用对象属性对应的 set 方法 ,完成成员变量的赋值,这种⽅式也称为 Set注入 。 img1

2. set注入详解

Set注入的变量类型:

  • JDK内置类型 8种基本类型 + String、数组类型、set集合、list集合、Map计划和、Properties集合。
  • 用户自定义类型

针对于不同类型的成员变量,在<property标签中,需要嵌套其他标签:

<property>
  xxxxx
</property>

img2

2.1 JDK内置类型

2.1.1 String+8种基本类型

<property name="id">
  <value>10</value>
</property>
<property name="name">
  <value>yusael</value>
</property>

2.1.2 数组

<property name="emails">
    <list>
        <value>[email protected]</value>
        <value>[email protected]</value>
        <value>[email protected]</value>
    </list>
</property>

2.1.3 Set集合

<property name="tels">
  <set>
    <value>138xxxxxxxxxx</value>
    <value>139xxxxxxxxxx</value>
    <value>138xxxxxxxxxx</value><!--set会自动去重-->
  </set>
</property>

2.1.4 List集合

<property name="addresses">
  <list>
    <value>China</value>
    <value>Earth</value>
    <value>hell</value>
  </list>
</property>

2.1.5 Map集合

<property name="qqs">
    <map>
        <entry>
            <key><value>hello</value></key>
            <value>12312312312</value>
        </entry>
        <entry>
            <key><value>world</value></key>
            <value>21314214214</value>
        </entry>
    </map>
</property>

2.1.6 Properites

<property name="p">
    <props>
        <prop key="key1">value1</prop>
        <prop key="key2">value2</prop>
        <prop key="key3">value3</prop>
    </props>
</property>

2.2 复杂JDK类型(Date、…)

需要程序员⾃定义类型转换器,处理。

2.3 构造注入开发

  • 提供有参构造⽅法

    public class Customer {
        private String name;
        private int age;
    
        public Customer(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Customer{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
        }
    
    }
    
  • spring 配置文件 #+BEGIN_SRC xml <bean id="customer" class="com.yusael.constructor.Customer"> <constructor-arg> <value>zhenyu</value> </constructor-arg> <constructor-arg> <value>21</value> </constructor-arg> </bean> #+BEGIN_SRC

2.4 构造方法重载

2.4.1 参数个数不同

参数个数不同时,通过控制 <constructor-arg> 标签的数量进⾏区分;

  1. 如果只有一个参数的话,只需要一对 <constructor-arg> 标签:

    <bean id="customer" class="com.yusael.constructor.Customer">
      <constructor-arg>
        <value>zhenyu</value>
      </constructor-arg>
    </bean>
    
  2. 如果有两个参数的话,用两对 <constructor-arg> 标签,以此类推。

    <bean id="customer" class="com.yusael.constructor.Customer">
      <constructor-arg>
        <value>zhenyu</value>
      </constructor-arg>
      <constructor-arg>
        <value>22</value>
      </constructor-arg>
    </bean>
    

2.4.2 参数相同

构造参数个数相同时,通过在标签引入 type 属性 进⾏类型的区分 <constructor-arg type="">

<bean id="customer" class="com.yusael.constructor.Customer">
  <constructor-arg type="int">
    <value>20</value>
  </constructor-arg>
</bean>

3. 注入总结

未来的实战中,应⽤ set注入 还是 构造注入? 答:set 注入更多。 1.构造注入麻烦(重载) 2.Spring 框架底层⼤量应⽤了 set注入。

4. 注入总图

img3

猜你喜欢

转载自www.cnblogs.com/instinct-em/p/13378862.html