Spring Bean injection method

Spring Bean injection method 1. Constructor


injection
1. Constructor injection, as the name suggests, is to implement a constructor in a program component, and the constructor can be one or more.
public class PersonServiceBean implements PersonServiceIF{
    //custom class
    private PersonDaoIF personDaoBean;
    //String type
    private String name;
    //collection type
    private List list;
    //Constructor (constructor injection must have this constructor)
    public PersonServiceBean(PersonDaoBean personDaoBean,String name,List list){
        this.personDaoBean = personDaoBean;
        this.name = name;
        this.list = list;
    }
}


<bean id="personDao" class="cn.glzaction.service.impl.PersonDaoBean"></bean >
<!--Constructor injection-->
<bean id="personService" class="cn.glzaction.service.impl.PersonServiceBean">
    <constructor-arg index="0" type="cn.glzaction.service.impl.PersonDaoBean" ref="personDao"/>
    <constructor-arg index="1" type="java.lang.String" value="glzaction"/>
    <constructor-arg index="2" type="java.util.List"> //Injection writing of List
        <list>
            <value>list1</value>
            <value>list2</value>
            <value>list3</value>
        </list>
    </constructor-arg>
</bean>


Constructor-arg: Indicates constructor injection
index: constructor parameter location
type: data type
ref: is a reference to a bean (another object)
value: is the value of this parameter (basic data type)
list: the value of List



II . Set value injection (setter injection)
1. Set value injection is to inject beans into components through the setXxxx method.
package cn.glzaction.service.impl;
import cn.glzaction.service.interfaces.PersonDaoIF;
import java.util. *;

public class PersonDaoBean implements PersonDaoIF {
    private String name;
    private Integer id;
    private List list;
    private Map map;

    public void setName(String name) { //The set method of the relevant parameters must be
        this.name = name;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public void setList(List list) {
        this.list = list;
    }
    public void setMap(Map map) {
        this.map = map;
    }
}


<bean id="personDao" class="cn.glzaction.service.impl.PersonDaoBean">
    <property name="name" type="java.lang.String" value="glzaction"/>
    <property name="id" type="java.lang.Integer" value="1"/>
    <property name="list" type="java.util.List">
        <list> //Injection writing of List
            <value>list1</value>
            <value>list2</value>
            <value>list3</value>
        </list>
    </property>
    <property name="map" type="java.util.Map">
        <map> //Injection writing of Map
            <entry key="key1" value="value1"></entry>
            <entry key="key2" value="value2"></entry>
        </map>
    </property>
</bean>


property: indicates setter injection
name: parameter name
type: data type
ref: is a reference to a bean (another object)
value: is the value of this parameter (basic data type)
list: value of List
map: value of Map



3 . Method injection of static factory (less used)

Fourth, method injection of instance factory (less used)



Refer to the original text: http://www.cnblogs.com/java-class/p/4727775.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326994972&siteId=291194637
Recommended