对于类成员变量,注入方式有三种:
1.构造函数注入
2.属性setter方法的属性注入
3.接口注入方式
Spring支持前两种方式

构造方法的属性注入

  • 通过构造方法注入Bean的属性值或依赖的对象,保证了Bean实例在是实例化后就可以使用。

  • 构造器注入在 < constructor-arg> 元素里声明

    public class User {
      private  String name;
      private String age;
    
      public User(String name, String age) {
          this.name = name;
          this.age = age;
      }
    
      @Override
      public String toString() {
          return "User{" +
                  "name='" + name + '\'' +
                  ", age='" + age + '\'' +
                  '}';
      }
    }

    xml中

    <bean id="user" class="com.imoo.ioc.demo4.User">
          <constructor-arg name="name" value="zhangsan"/>
          <constructor-arg name="age" value="23"/>
      </bean>

    测试类中

    public class SpringDemo4 {
      @Test
      public  void demo1() {
          ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
          User user = (User)applicationContext.getBean("user");
          System.out.println(user);
      }
    }

    set方法注入(更倾向用这种方式)

    在配置文件中,使用< property>标签,设置注入的属性

    <bean id="person" class="com.imoo.ioc.demo4.Person">
          <property name="name" value="lisi"/>
          <property name="age" value="32"/>
      </bean>

对于对象类型的属性注入
先写一个Cat的class类,并添加set get方法
在配置文件中,先添加cat的bean,然后在person的bean中注入cat的属性。
值添加使用的的ref而不是value

<bean id="person" class="com.imoo.ioc.demo4.Person">
        <property name="name" value="lisi"/>
        <property name="age" value="32"/>
        <property name="cat" ref="cat"/>
    </bean>

    <bean id="cat" class="com.imoo.ioc.demo4.Cat">
        <property name="name" value="ketty"/>
    </bean>

普通类型的值用value ,对象类型的值用ref

p名称空间的属性注入

  • 为了简化xml文件配置,Spring从2.5开始引入一个新的p名称空间
  • p:< 属性名> = "xxx" 引入常量值
  • p:< 属性名> - ref = "xxx" 引入其他Bean对象

-先配置xml :xmlns:p="http://www.springframework.org/schema/p"

<bean id="person" class="com.imoo.ioc.demo4.Person" p:name="大黄" p:age="32"p:cat-ref="cat"/>
    <bean id="cat" class="com.imoo.ioc.demo4.Cat" p:name="小黄"/>

SpEL注入

  • SpEL:spring expression language,spring表达式语言,对依赖注入进行简化
  • 属性注入比较复杂的时候使用
  • 语法: #{表达式}
  • < bean id="" value = "#{表达式} >
    图片说明

复杂类型的属性注入

<!--复杂类型的属性注入-->
    <bean id="collectionBean" class="com.imoo.ioc.demo5.CollectionBean">
        <property name="arrs">
            <list>
                <value>aaa</value>
                <value>bbb</value>
            </list>
        </property>

        <property name="list">
            <list>
                <value>111</value>
                <value>222</value>

            </list>
        </property>

        <property name="set">
            <set>
                <value>ddd</value>
                <value>eee</value>
                <value>fff</value>
            </set>
        </property>

        <property name="map">
            <map>
                <entry key="aaa" value="111"></entry>
                <entry key="bbb" value="222"></entry>
                <entry key="ccc" value="333"></entry>
            </map>
        </property>

        <property name="properties">
            <props>
                <prop key="username">root</prop>
                <prop key="password">root</prop>
            </props>
        </property>
    </bean>