Spring依赖注入的四种方式

Spring依赖注入的四种方式

首先,我们需要定义一个Bean的class类:

package framework.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

public class Person  {

    private String name;
    private String address;
    private int phone;


    public Person() {
        System.out.println("【构造器】调用Person的构造器实例化");
    }


    public Person(String name, String address, int phone) {
        this.name = name;
        this.address = address;
        this.phone = phone;
    }

    public String getBeanName() {
        return beanName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("【注入属性】注入属性name");
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        System.out.println("【注入属性】注入属性address");
        this.address = address;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        System.out.println("【注入属性】注入属性phone");
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name="" + name + """ +
                ", address="" + address + """ +
                ", phone=" + phone +
                ", beanFactory=" + beanFactory +
                ", beanName="" + beanName + """ +
                "}";
    }

}

Setter注入

    <!--    setter -->
    <bean id="person1" class="framework.spring.Person" scope="singleton" p:name="张三" p:address="广州"
          p:phone="1319412212" />

setter注入只需要将相对应的属性参数传入,就可以了。

构造器注入

    <!-- constructor-->
    <bean id="person2" class="framework.spring.Person" init-method="myInit"
          destroy-method="myDestory" scope="singleton"  >
        <constructor-arg name="name" value="zhangsan"></constructor-arg>
        <constructor-arg name="address" value="beijing"></constructor-arg>
        <constructor-arg name="phone" value="1319124212"></constructor-arg>
    </bean>

静态工厂注入

    <!-- static factory-->
    <bean id="person3" class="framework.spring.PersonFactory"  scope="singleton"  factory-method="staticPersonFactory">
    </bean>

在静态工厂注入的配置文件中,我们需要定义对应的静态工厂类和静态方法。注意这里的静态就是指static,我刚刚突然想不起来静态是指什么意思了。

public class PersonFactory {
    // static factory
    public static Person staticPersonFactory(){
        return new Person("lisi","nanjing",131912312);
    }

    // factory
    public Person personFactory(){
        return new Person("wanger","changsha",131912312);
    }
}

这里定义了两个工厂,一个是静态的,一个是非静态的。

实例工厂注入

实例工厂和静态工厂稍微有点区别,因为静态工厂不需要初始化工厂,而实例工厂需要。

    <!-- factory-->
    <bean id="personFactory" class="framework.spring.PersonFactory"></bean>
    <bean id="person4" class="framework.spring.PersonFactory"  scope="singleton"  factory-bean="personFactory" factory-method="personFactory">
    </bean>

这里我们先定义了一个工厂类,然后才定义的我们需要的bean对象。

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » Spring依赖注入的四种方式