spring属性依赖注入

一、构造方法方式注入

1、项目结构如下:

 

2、新建Customer类

package hjp.spring.attributeinject;

public class Customer {
    private String name;
    private Integer age;
    private String city;

    public Customer() {

    }

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

    public Customer(Integer age, String city) {
        this.age = age;
        this.city = city;
    }

    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 getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Customer [name=" + name + ", age=" + age + ", city=" + city + "]";
    }
}
Cusomer

3、新建beans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                              http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 如果没有配置 constructor-arg节点,则使用无参构造器-->
    <bean id="customerId" class="hjp.spring.attributeinject.Customer">
    <!--constructor-arg 配置构造参数
        index 表示参数索引号
        type 设置参数数据类型
        value 设置普通数据
        ref 设置引用数据
        
        如果只使用index和value,而不指定数据类型,则默认匹配符合条件的第一个构造函数
        如果配置了type,那么索引处的数据类型要对应正确
      -->
    <constructor-arg index="0" value="23" type="java.lang.Integer"></constructor-arg>
    <constructor-arg index="1" value="Tom" type="java.lang.String"></constructor-arg>
    </bean>
</beans>

4、新建测试类

package hjp.spring.attributeinject;

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

public class TestApp {
    @Test
    public void demo1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "hjp/spring/attributeinject/beans.xml");
        Customer customer = applicationContext.getBean("customerId", Customer.class);
        System.out.println(customer);
    }
}

二、setter方法注入

1、新增类Contact

package hjp.spring.attributeinject;

public class Contact {
    private String address;
    private String telphone;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTelphone() {
        return telphone;
    }

    public void setTelphone(String telphone) {
        this.telphone = telphone;
    }

    @Override
    public String toString() {
        return "Contact [address=" + address + ", telphone=" + telphone + "]";
    }
}
Contact

2、为Customer类新增属性contact,新增构造函数

public Customer(String name,Integer age,Contact contact){
this.name=name;
this.age=age;
this.contact=contact;
}

package hjp.spring.attributeinject;

public class Customer {
    private String name;
    private Integer age;
    private String city;
    
    private Contact contact;

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public Customer() {

    }

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

    public Customer(Integer age, String city) {
        this.age = age;
        this.city = city;
    }
    
    public Customer(String name,Integer age,Contact contact){
        this.name=name;
        this.age=age;
        this.contact=contact;
    }

    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 getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Customer [name=" + name + ", age=" + age + ", city=" + city + ", contact=" + contact + "]";
    }
}
Customer

3、更改beans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                              http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="customerId" class="hjp.spring.attributeinject.Customer">
    <property name="name" value="Tom"></property>
    <property name="age" value="23"></property>
    <property name="contact" ref="contactId"></property>
    </bean>
    <bean id="contactId" class="hjp.spring.attributeinject.Contact">
    <property name="address" value="北京"></property>
    <property name="telphone" value="12345678"></property>
    </bean>
</beans>

4、测试类不变

三、集合注入

1、项目结构:

2、新建CollectionTest类

package hjp.spring.attributeinject.collection;

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

public class CollectionTest {
    private List<String> listData;
    private Set<String> setData;
    private Map<String, String> mapData;
    private String[] arrayData;
    private Properties propsData;

    public List<String> getListData() {
        return listData;
    }

    public void setListData(List<String> listData) {
        this.listData = listData;
    }

    public Set<String> getSetData() {
        return setData;
    }

    public void setSetData(Set<String> setData) {
        this.setData = setData;
    }

    public Map<String, String> getMapData() {
        return mapData;
    }

    public void setMapData(Map<String, String> mapData) {
        this.mapData = mapData;
    }

    public String[] getArrayData() {
        return arrayData;
    }

    public void setArrayData(String[] arrayData) {
        this.arrayData = arrayData;
    }

    public Properties getPropsData() {
        return propsData;
    }

    public void setPropsData(Properties propsData) {
        this.propsData = propsData;
    }

    @Override
    public String toString() {
        return "CollectionText [
listData=" + listData + ", 
setData=" + setData + ", 
mapData=" + mapData
                + ", 
arrayData=" + Arrays.toString(arrayData) + ", 
propsData=" + propsData + "
]";
    }
}
CollectionTest

3、新建beans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                              http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 集合内部,普通值用value,引用值用ref -->
    <bean id="collectionTestId" class="hjp.spring.attributeinject.collection.CollectionTest">
        <!-- list集合 -->
        <property name="listData">
            <list>
                <value>Jim</value>
                <value>Tom</value>
                <value>Jerry</value>
            </list>
        </property>
        <!-- set集合 -->
        <property name="setData">
            <set>
                <value>张三</value>
                <value>李四</value>
                <value>王五</value>
            </set>
        </property>
        <!-- map集合 -->
        <property name="mapData">
            <map>
                <!-- 第一种写法 -->
                <entry key="A" value="a"></entry>
                <entry key="B" value="b"></entry>
                <entry key="C" value="c"></entry>
                <!-- 第二种写法 -->
                <entry>
                    <key>
                        <value>D</value>
                    </key>
                    <value>d</value>
                </entry>
            </map>
        </property>
        <!-- 数组 -->
        <property name="arrayData">
            <array>
                <value>a</value>
                <value>b</value>
                <value>c</value>
            </array>
        </property>
        <!-- Properties -->
        <property name="propsData">
            <props>
                <prop key="X">x</prop>
                <prop key="Y">y</prop>
                <prop key="Z">z</prop>
            </props>
        </property>
    </bean>
</beans>
beans

4、新建测试类

package hjp.spring.attributeinject.collection;

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

public class TestApp {
    @Test
    public void demo1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "hjp/spring/attributeinject/collection/beans.xml");
        CollectionTest collectionTest = applicationContext.getBean("collectionTestId", CollectionTest.class);
        System.out.println(collectionTest);
    }
}
原文地址:https://www.cnblogs.com/hujiapeng/p/4779164.html