victory的博客

长安一片月,万户捣衣声

0%

spring | 属性赋值

属性赋值-字面量和ref

spring管理对象时,可以使用标签给对象的属性赋值。
1.字面量


…..
2.外部已声明的bean引用其他的bean(使用ref属性)
3.内部bean
4.集合属性赋值(list/array/set)
示例:
1.项目目录

2.Student.java

package com.atguigu.spring.di;

public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private String sex;
    private Double score;
    private Teacher teacher;
    
    
    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public Double getScore() {
        return score;
    }
    
    public void setScore(Double score) {
        this.score = score;
    }
    
    public Integer getId() {
        return id;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public Integer getAge() {
        return age;
    }
    
    public void setAge(Integer age) {
        this.age = age;
    }
    
    public String getSex() {
        return sex;
    }
    
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", score=" + score
                + ", teacher=" + teacher + "]";
    }

    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public Student(Integer id, String name, Integer age, String sex) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    
    public Student(Integer id, String name, Double score, String sex){
        this.id = id;
        this.name = name;
        this.score = score;
        this.sex = sex;
    }
}

3.Teacher.java

package com.atguigu.spring.di;

import java.util.List;
import java.util.Map;

public class Teacher {
    private Integer tid;
    
    private String tname;
    
    private List<String> cls;
    
    private List<Student> students;
    
    private Map<String, String> bossMap;
    
    public Map<String, String> getBossMap() {
        return bossMap;
    }
    public void setBossMap(Map<String, String> bossMap) {
        this.bossMap = bossMap;
    }
    public List<Student> getStudents() {
        return students;
    }
    public void setStudents(List<Student> students) {
        this.students = students;
    }
    public List<String> getCls() {
        return cls;
    }
    public void setCls(List<String> cls) {
        this.cls = cls;
    }
    public Integer getTid() {
        return tid;
    }
    public void setTid(Integer tid) {
        this.tid = tid;
    }
    public String getTname() {
        return tname;
    }
    public void setTname(String tname) {
        this.tname = tname;
    }
    @Override
    public String toString() {
        return "Teacher [tid=" + tid + ", tname=" + tname + ", cls=" + cls + ", students=" + students + ", bossMap="
                + bossMap + "]";
    }
    
    
}

5.beans-di.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <bean id="s1" class="com.atguigu.spring.di.Student">
        <!-- 通过set方法注入 -->
        <property name="id" value="10010"></property>
        <!-- 
            <property name="id">
                <value>10010</value>
            </property>
         -->
         
        <property name="name" value="张三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
    </bean>
        
    <bean id="s2" class="com.atguigu.spring.di.Student">
        <!-- 通过构造方法注入 -->
        <constructor-arg value="10086"></constructor-arg>
        <constructor-arg value="李四"></constructor-arg>
        <constructor-arg value="24"></constructor-arg>
        <constructor-arg value="女"></constructor-arg>
    </bean>
    
    <bean id="s3" class="com.atguigu.spring.di.Student">
        <!-- 通过构造方法注入 -->
        <constructor-arg value="10022"></constructor-arg>
        <constructor-arg value="王五"></constructor-arg>
        <constructor-arg value="90" index="2" type="java.lang.Double"></constructor-arg>
        <constructor-arg value="女"></constructor-arg>
    </bean>
    
    <!-- Namespaces勾选p(beans标签会增加内容xmlns:p="http://www.springframework.org/schema/p") -->
    <bean id="s4" class="com.atguigu.spring.di.Student" p:id="10033" p:name="赵六" p:age="26" p:sex="男" p:teacher-ref="teacher"></bean>
    
    <!-- 给bean的级联属性赋值 -->
    <bean id="s5" class="com.atguigu.spring.di.Student">
        <property name="id" value="10055"></property>
        <property name="name" value="张三三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
        <property name="teacher" ref="teacher"></property>
        
        <!-- 级联测试 给bean的级联属性赋值-->
        <property name="teacher.tname" value="小红"></property>
    </bean>
        
    <!-- 外部已声明的bean、引用其他的bean -->
    <bean id="teacher" class="com.atguigu.spring.di.Teacher">
        <property name="tid" value="10000"></property>
        <property name="tname" value="小明"></property>
    </bean>
    
    <!-- 内部bean -->
    <bean id="s6" class="com.atguigu.spring.di.Student">
        <property name="id" value="10066"></property>
        <property name="name" value="崔八"></property>
        <property name="age" value="18"></property>
        <property name="sex" value="男"></property>
        <property name="teacher">
            <bean id="tt" class="com.atguigu.spring.di.Teacher">
                <property name="tid" value="2222"></property>
                <property name="tname" value="admin"></property>
            </bean>
        </property>
    </bean>
    <!-- 定义在某个bean内部的bean,只能在当前bean中使用 -->
    <!-- 
        <bean id="s7" class="com.atguigu.spring.di.Student">
            <property name="id" value="10066"></property>
            <property name="name" value="崔八"></property>
            <property name="age" value="18"></property>
            <property name="sex" value="男"></property>
            <property name="teacher" ref="tt">
            </property>
        </bean>
     -->
     
     <!-- list属性赋值1 -->
     <bean id="t1" class="com.atguigu.spring.di.Teacher">
        <property name="tid" value="0000"></property>
        <property name="tname" value="佟老师"></property>
        <property name="cls">
            <list>
                <value>A</value>
                <value>B</value>
                <value>C</value>
            </list>
        </property>
     </bean>
     
     <!-- list属性赋值2 -->
     <bean id="t2" class="com.atguigu.spring.di.Teacher">
        <property name="tid" value="10002"></property>
        <property name="tname" value="婷姐"></property>
        <property name="students">
            <list>
                <ref bean="s1"/>
                <ref bean="s2"/>
                <ref bean="s3"/>
            </list>
        </property>
     </bean>
     
     <!-- array属性赋值 -->
     <!-- set属性赋值 -->
     
     <!-- map属性赋值 -->
     <bean id="t3" class="com.atguigu.spring.di.Teacher">
        <property name="tid" value="10003"></property>
        <property name="tname" value="admin"></property>
        <property name="bossMap">
            <map>
                <entry>
                    <key>
                        <value>10001</value>
                    </key>
                    <value>佟老师</value>
                </entry>
                
                <entry>
                    <key>
                        <value>10002</value>
                    </key>
                    <value>陈老师</value>
                </entry>
            </map>
        </property>
     </bean>
     
     <bean id="t4" class="com.atguigu.spring.di.Teacher">
        <property name="tid" value="10004"></property>
        <property name="tname" value="root"></property>
        <property name="students" ref="students"></property>
     </bean>
    
    <util:list id="students">
        <ref bean="s4"/>
        <ref bean="s5"/>
        <ref bean="s6"/>
    </util:list>
    
    <util:map id="map">
        <entry>
            <key>
                <value>1</value>
            </key>
            <value>张三</value>
        </entry>
    </util:map>
</beans>

6.Test.java

package com.atguigu.spring.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans-di.xml");
        
        Student s1 = ac.getBean("s1", Student.class);
        Student s2 = ac.getBean("s2", Student.class);
        Student s3 = ac.getBean("s3", Student.class);
        Student s4 = ac.getBean("s4", Student.class);
        Student s5 = ac.getBean("s5", Student.class);
        Student s6 = ac.getBean("s6", Student.class);
        Teacher t1 = ac.getBean("t1", Teacher.class);
        Teacher t2 = ac.getBean("t2", Teacher.class);
        Teacher t3 = ac.getBean("t3", Teacher.class);
        Teacher t4 = ac.getBean("t4", Teacher.class);
        
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
        System.out.println(s5);
        System.out.println(s6);
        System.out.println(t1);
        System.out.println(t2);
        System.out.println(t3);
        System.out.println(t4);
    }
}