spring属性注入

set方法属性注入

给类中的属性提供set方法

实体类: 提供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="com.itlike.demo4.Student">
        <property name="name" value="aaa"/>
        <property name="age" value="12"/>
    </bean>

测试类及结果:

 public void test1(){
        ClassPathXmlApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) applicationContext.getBean("student");
        System.out.println(student.name+" "+student.age);
    }

    
//aaa 12

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

实体类:有参的构造方法

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="com.itlike.demo4.Student">
        <constructor-arg name="name" value="bbb"/>
        <constructor-arg name="age" value="22"/>
    </bean>

测试类及结果:

  public void test1(){
        ClassPathXmlApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) applicationContext.getBean("student");
        System.out.println(student.name+" "+student.age);
    }
//bbb 22

set方法设置对象类型的属性 

将对象属性同样交给spring管理,然后ref该对象

P名称空间属性注入

(1)引入约束:

xmlns:p="http://www.springframework.org/schema/p"

(2)改写配置文件:

<bean id="dog" class="com.itlike.demo4.Dog" p:name="狗子" p:age="5" p:color="白色">
        <!--<property name="name" value="旺财"/>
        <property name="age" value="10"/>
        <property name="color" value="red"/>-->
     </bean>

 spEL表达式属性注入

 实体类:提供set方法

public class Dog {
    public String name;
    public Integer age;
    public String color;

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

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }
}

配置文件:字符串用单引号引起来  

<bean id="dog2" class="com.itlike.demo4.Dog" >
        <property name="name" value="#{'旺财'}"/>
        <property name="age" value="#{10}"/>
        <property name="color" value="#{'red'}"/>
    </bean>

 集合类型的属性注入

数组      list集合   set集合    map集合

实体类:

public class Dog {
    public String name;
    public Integer age;
    public String color;

    //集合类型
    //数组
    public String[] arr;
    //list
    public List list;
    //set
    public Set set;
    //map
    public HashMap map;

    public void setMap(HashMap map) {
        this.map = map;
    }

    public void setSet(Set set) {
        this.set = set;
    }

    public void setList(List list) {
        this.list = list;
    }

    public void setArr(String[] arr) {
        this.arr = arr;
    }

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

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                ", arr=" + Arrays.toString(arr) +
                ", list=" + list +
                ", set=" + set +
                ", map=" + map +
                '}';
    }
}

配置文件:

 <bean id="dog2" class="com.itlike.demo4.Dog" >
        <property name="name" value="#{'旺财'}"/>
        <property name="age" value="#{10}"/>
        <property name="color" value="#{'red'}"/>
        <property name="arr">
            <list>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </list>
        </property>

        <property name="list">
            <list>
                <value>123</value>
                <value>abc</value>
            </list>
        </property>

        <property name="set">
            <set>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </set>
        </property>

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

测试类:

 public void test1(){
        ClassPathXmlApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) applicationContext.getBean("student");
        System.out.println(student.dog);
    }


//Dog{name='旺财', age=10, color='red', arr=[aaa, bbb, ccc], list=[123, abc], set=[1, 2, 3], map={key1=value1, key2=value2}}

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值