其他分享
首页 > 其他分享> > spring - ioc - 配置文件注入属性

spring - ioc - 配置文件注入属性

作者:互联网

application.xml

    <!--ioc注入属性的两种方式:构造器注入与set方法注入 | 注入基本数据类型与引用数据类型-->
    <!--1.无参构造器创建对象-->
    <bean id="human" class="com.levi.dao.Human"/>
    <!--2.有参构造器创建对象-->
    <bean id="user" class="com.levi.dao.User">
        <!--构造器注入:基本数据类型与引用数据类型-->
        <constructor-arg name="name" value="levi"/>
        <constructor-arg name="id" value="12"/>
        <constructor-arg name="human" ref="human"/>
        <!--set方法注入:基本数据类型与引用数据类型-->
        <property name="id" value="12"/>
        <property name="name" value="levi"/>
        <property name="human" ref="human"/>
    </bean>

    <!--3.注入集合-->
    <bean id="human_test" class="com.levi.dao.Human">
        <!--3.1 list集合-->
        <property name="list">
            <list>
                <value>1</value>
                <ref bean="human"/>
            </list>
        </property>
        <!--3.2 map集合-->
        <property name="map">
            <map>
                <entry key="1" value="1"/>
                <entry key-ref="human" value-ref="human"/>
            </map>
        </property>
        <!--3.3 set集合-->
        <property name="set">
            <set>
                <value>1</value>
                <ref bean="human"/>
            </set>
        </property>
        <!--3.4 数组-->
        <property name="array">
            <array>
                <value>1</value>
                <ref bean="human"/>
            </array>
        </property>
    </bean>

User

package com.levi.dao;

public class User {
    /*基本数据类型·*/
    private int id;
    private String name;
    /*引用数据类型*/
    private Human human;

    public User(int id, String name, Human human) {
        this.id = id;
        this.name = name;
        this.human = human;
    }

    public void setId(int id) {
        this.id = id;
    }

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

    public void setHuman(Human human) {
        this.human = human;
    }
}

Human

package com.levi.dao;

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

public class Human {
    private List list;
    private Map map;
    private Set set;
    private Object[] array;

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

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

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

    public void setArray(Object[] array) {
        this.array = array;
    }
}

 

标签:name,配置文件,spring,void,private,id,human,ioc,public
来源: https://www.cnblogs.com/leviAckman/p/15661370.html