Spring属性注入的几种方式详解

项目路径:https://gitee.com/wuhan1/spring-parent.git 下的spring-01
给各种类型的对象赋值
Student类和MathTeacher类

package com.xqc;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Student {
    //学号
    private Integer stuNo;
    //姓名
    private String stuName;
    //爱好
    private List<String> hobbies;
    //性别
    private String[] sexs[];
    //map
    private Map<String,String> mapElement;
    //set
    private Set<String> stEment;
    //数学老师
    private MathTeacher mathTeacher;
    //赋null值
    private String stNull;
    //赋值""
    private String str;

    public Integer getStuNo() {
        return stuNo;
    }

    public void setStuNo(Integer stuNo) {
        this.stuNo = stuNo;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public void setSexs(String[][] sexs) {
        this.sexs = sexs;
    }

    public void setMapElement(Map<String, String> mapElement) {
        this.mapElement = mapElement;
    }

    public MathTeacher getMathTeacher() {
        return mathTeacher;
    }

    public void setMathTeacher(MathTeacher mathTeacher) {
        this.mathTeacher = mathTeacher;
    }

    public Set<String> getStEment() {
        return stEment;
    }

    public void setStEment(Set<String> stEment) {
        this.stEment = stEment;
    }

    public Student(Integer stuNo, String stuName) {
        this.stuNo = stuNo;
        this.stuName = stuName;
    }

    public Student() {
    }

    public String getStNull() {
        return stNull;
    }

    public void setStNull(String stNull) {
        this.stNull = stNull;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuNo=" + stuNo +
                ", stuName='" + stuName + '\'' +
                ", hobbies=" + hobbies +
                ", sexs=" + Arrays.toString(sexs) +
                ", mapElement=" + mapElement +
                ", stEment=" + stEment +
                ", mathTeacher=" + mathTeacher +
                ", stNull='" + stNull + '\'' +
                ", str='" + str + '\'' +
                '}';
    }
}
package com.xqc;
//数学老师
public class MathTeacher {
    //姓名
    private String tccName;
    //年龄
    private Integer age;

    public String getTccName() {
        return tccName;
    }

    public void setTccName(String tccName) {
        this.tccName = tccName;
    }

    public Integer getAge() {
        return age;
    }

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

    public MathTeacher(String tccName, Integer age) {
        this.tccName = tccName;
        this.age = age;
    }

    public MathTeacher() {
    }

    @Override
    public String toString() {
        return "MathTeacher{" +
                "tccName='" + tccName + '\'' +
                ", age=" + age +
                '}';
    }
}

1.set方式,applicationContext配置

<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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd">

    <!--set方式,通过反射,调用set方法-->
    <bean id="student" class="com.xqc.Student">
        <property name="stuNo" value="1111110" ></property>
        <!--普通对象赋值-->
        <property name="stuName" value="张三"></property>
        <!--集合list赋值-->
        <property name="hobbies">
            <list>
                <value>足球</value>
                <value>篮球球</value>
                <value>羽毛球</value>
            </list>
        </property>
        <!--集合set赋值-->
        <property name="stEment">
            <set>
                <value>呵呵</value>
                <value>嘿嘿</value>
            </set>
        </property>

        <!--数组赋值-->
        <property name="sexs">
            <array>
                <value>男</value>
                <value>女</value>
            </array>
        </property>
        <!--集合map赋值-->
        <property name="mapElement">
            <map>
                <entry>
                    <key>
                        <value>st1</value>
                    </key>
                    <value>小学</value>
                </entry>
                <entry>
                    <key>
                        <value>st2</value>
                    </key>
                    <value>中学</value>
                </entry>
            </map>
        </property>

        <!--赋null值-->
        <property name="stNull">
            <null />
        </property>

        <!--赋空字符串值-->
        <property name="str">
            <value></value>
        </property>

        <!--对象赋值-->
        <property name="mathTeacher" ref="mathTccher"></property>
    </bean>

    <bean id="mathTeacher" class="com.xqc.MathTeacher">
        <property name="tccName" value="马老师"></property>
        <property name="age" value="28"></property>
    </bean>


</beans>

其中List,Set,数组都可以用list的方式赋值,但最好各自对应


 

2.构造器方式,可以指定name属性或者index

    <!--构造器方式-->
    <bean id="student2" class="com.xqc.Student">
        <!--        
        <constructor-arg value="111119" name="stuNo" ></constructor-arg>
        <constructor-arg value="赵老师" name="stuName"></constructor-arg>
        -->
        <constructor-arg value="111119" index="0" ></constructor-arg>
        <constructor-arg value="赵老师" index="1"></constructor-arg>
    </bean>

3.p名称空间注入,需要引入p标签空间

    <!--p名称空间注入-->
    <bean id="student3" class="com.xqc.Student" p:stuNo="14444" p:stuName="黄老师"></bean>


4.自动装配,使用autowire

       <bean id="mathTeacher" class="com.xqc.MathTeacher">
        <property name="tccName" value="马老师"></property>
        <property name="age" value="28"></property>
    </bean>

     <!--自动注入,一般主要用于对象,简单类型是不能用的,会降低可读性,不建议用,byName|byType-->
    <bean id="student4" class="com.xqc.Student" autowire="byName">
        <property name="stuNo" value="99888"></property>
    </bean>

byName:本质就是通过ID,在容器中找bean的id与student中的引用类型mathTeacher一致的名称
byType:会自动根据autowire找到容器中的其他bean,是否与student中的引用类型MathTeacher类型一致的,有则自动注入,如果有重复的则报错,比如





 

猜你喜欢

转载自blog.csdn.net/dhj199181/article/details/108590485
今日推荐