欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring实例化bean的方式

程序员文章站 2022-05-21 15:05:52
...

一、通过set方法实例化bean(set注入、使用property标签赋值)

bean:

package scope;

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

public class bean2 {
    private Integer id;
    private String name;
    private String[] fatherAndMother;
    private double source;
    private Map<String,String> shu;
    private List<String> home;
    private Set<String> xueLi;
    private bean1 bean;

    public bean2() {
    }

    public bean2(Integer id, String name, String[] fatherAndMother, double source, Map<String, String> shu, List<String> home, Set<String> xueLi, bean1 bean) {
        this.id = id;
        this.name = name;
        this.fatherAndMother = fatherAndMother;
        this.source = source;
        this.shu = shu;
        this.home = home;
        this.xueLi = xueLi;
        this.bean = bean;
    }

    @Override
    public String toString() {
        return "bean2{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ",\n fatherAndMother=" + Arrays.toString(fatherAndMother) +
                ",\n source=" + source +
                ",\n shu=" + shu +
                ",\n home=" + home +
                ",\n xueLi=" + xueLi +
                ",\n bean=" + bean +
                '}';
    }

    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 String[] getFatherAndMother() {
        return fatherAndMother;
    }

    public void setFatherAndMother(String[] fatherAndMother) {
        this.fatherAndMother = fatherAndMother;
    }

    public double getSource() {
        return source;
    }

    public void setSource(double source) {
        this.source = source;
    }

    public Map<String, String> getShu() {
        return shu;
    }

    public void setShu(Map<String, String> shu) {
        this.shu = shu;
    }

    public List<String> getHome() {
        return home;
    }

    public void setHome(List<String> home) {
        this.home = home;
    }

    public Set<String> getXueLi() {
        return xueLi;
    }

    public void setXueLi(Set<String> xueLi) {
        this.xueLi = xueLi;
    }

    public bean1 getBean() {
        return bean;
    }

    public void setBean(bean1 bean) {
        this.bean = bean;
    }
}

在xml中对bean进行注入

	<bean class="scope.bean2" id="bean2">

        <!--当属性类型为基本数据类型的时候,赋值使用value-->
        <property name="id" value="13"></property>
        <property name="source" value="13.14"></property>
        <property name="name" value="张三"></property>


        <!--当属性为另外的一个bean的实例的时候,使用ref进行赋值,ref的值为对应bean的ID-->
        <property name="bean" ref="bean1"></property>


        <!--给list类型赋值的时候,使用内部标签进行赋值-->
        <!--当使用标签进行赋值的时候不需要再标签中为字符串加双引号-->
        <property name="home">
            <list>
                <value>张三</value>
                <value>李四</value>
                <value>王五</value>
                <value>麻子</value>
            </list>
        </property>
        
        
        <!--使用array标签为数组赋值-->
        <property name="fatherAndMother">
            <array>
                <value>阿大</value>
                <value>阿二</value>
            </array>
        </property>

        
        <!--使用map标签为map类型赋值,一个entry标签中包含了一个键值对-->
        <property name="shu">
            <map>
                <entry>
                    <key>
                        <value>阿尔法</value>
                    </key>
                    <value>作者:王五</value>
                </entry>

                <entry>
                    <key>
                        <value>思途来登</value>
                    </key>
                    <value>作者:麻子</value>
                </entry>
            </map>
        </property>

        
        <!--使用set标签为set类型赋值-->
        <property name="xueLi">
            <set>
                <value>我们都是中国人</value>
                <value>健健康康成长</value>
            </set>
        </property>
    </bean>



二、通过构造方法实例化bean(构造方法注入、使用)

bean:

package scope;

public class bean3 {
    private int age;
    private String name;

    public bean3() {
    }

    public bean3(int age, String name) {
        this.age = age;
        this.name = name;
    }

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

}

在xml中对bean进行注入:

<bean class="scope.bean3" id="bean23">
        <constructor-arg value="12"></constructor-arg>
        <constructor-arg value="zhangsan"></constructor-arg>
   </bean>

如果constructor-arg与构造器顺序不一致,可以通过index或者name属性进行调整。


三、使用标签进行注入(c标签、p标签)

bean:

package scope;

public class bean3 {
    private int age;
    private String name;

    public bean3() {
    }

    public bean3(int age, String name) {
        this.age = age;
        this.name = name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
  1. 加入c标签和p标签的命名空间
    	xmlns:c="http://www.springframework.org/schema/c"
        xmlns:p="http://www.springframework.org/schema/p"
    
  2. 使用c标签进行赋值(constructor-arg,使用c标签也就是使用构造方法进行注入,所以必须要有对应的构造方法)
    <bean class="scope.bean3" id="bean3"
    c:age="12"
    c:name="张三"/>
    
  3. 使用p标签进行赋值(property,使用p标签也就是使用set注入,bean必须要有set方法)
    <bean class="scope.bean3" id="bean3"
    p:age="12"
    p:name="李四"
    />