Spring之属性注入

一、构造方法的方式的属性注入

public class Student {
    public String name;
    public Integer age;

    public Student(String name, Integer age){
        this.name = name;
        this.age = age;
    }
}
<bean id="student" class="pers.liuchengyin.demo4.Student">
        <constructor-arg name="name" value="柳成荫"/>
        <constructor-arg name="age" value="16"/>
</bean>

 二、Set方法的普通类型属性注入

public class Student {
    public String name;
    public Integer age;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
<bean id="student" class="pers.liuchengyin.demo4.Student">
        <property name="name" value="柳成荫"></property>
        <property name="age" value="10"></property>
</bean>

三、Set方法设置对象类型属性注入

public class Student {
    public String name;
    public Integer age;
    public Dog dog;


    public Student(String name, Integer age){
        this.name = name;
        this.age = age;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }
}
<bean id="dog" class="pers.liuchengyin.demo4.Dog">
     <property name="name" value="金毛"></property>
</bean>

<bean id="student" class="pers.liuchengyin.demo4.Student">
     <constructor-arg name="name" value="柳成荫"/>
     <constructor-arg name="age" value="16"/>
     <!-- 对象是通过ref的方式 -->
     <property name="dog" ref="dog"></property>
</bean>

 四、P名称空间的属性注入

<!-- 使用前,先添加名称空间 -->
xmlns:p="http://www.springframework.org/schema/p"
<!-- 使用p名称空间,就不需要写那那么多的property -->
<bean id="dog" class="pers.liuchengyin.demo4.Dog" p:age="3" p:color="黄色" p:name="金毛">
</bean>

<bean id="student" class="pers.liuchengyin.demo4.Student" p:dog-ref="dog">
    <constructor-arg name="name" value="柳成荫"/>
    <constructor-arg name="age" value="16"/>
    <property name="dog" ref="dog"></property>
</bean>

五、spEL表达式的属性注入

<bean id="dog2" class="pers.liuchengyin.demo4.Dog" >
    <property name="name" value="#{'哈士奇'}"/>
    <property name="age" value="#{10}"/>
    <property name="color" value="#{'黑白'}"/>
</bean>

<bean id="student" class="pers.liuchengyin.demo4.Student">
    <constructor-arg name="name" value="柳成荫"/>
    <constructor-arg name="age" value="16"/>
    <property name="dog" value="#{dog2}"/>
    <property name="dogName" value="#{dog2.name}"/>
</bean>

六、集合类型属性注入

①数组

<!-- attr是数组名 -->
<property name="attr">
    <list>
        <value>aaa</value>
        <value>bbb</value>
        <value>ccc</value>
    </list>
</property>	

②List集合

<!-- list是集合名 -->
<property name="list">
    <list>
        <value>123</value>
        <value>456</value>
        <value>789</value>
    </list>
</property>	

 ③Set集合

<property name="set">
    <set>
        <value>123</value>
        <value>456</value>
        <value>789</value>
    </set>
</property>

 ④Map集合

<property name="map">
    <map>
        <entry key="key1" value="value1"></entry>
        <entry key="key2" value="value2"></entry>
        <entry key="key3" value="value3"></entry>
    </map>
</property>		

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值