跳转至

Spring 基于设值函数的依赖注入

1. 介绍

  当容器调用一个无参的构造函数或一个无参的静态 factory 方法来初始化你的 bean 后,通过容器在你的 bean 上调用设值函数,基于设值函数的 DI 就完成了。

2. 示例

ioc3.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--<bean id="person" class="com.cmz.bean.Person" scope="prototype">-->
    <!--根据属性值设置的时候,name的名字取决于set方法后面的参数首字母小写的名称-->
    <!--
    name: 表示参数列表的名称
    value: 表示实际的具体指
    type: 表示值的类型
    index: 表示值的下标,从0开始
    -->

    <bean id="person" class="com.cmz.bean.Person" init-method="init" destroy-method="destroy">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="20"></property>
        <property name="gender" value="男"></property>
        <property name="date" value="2020/02/28"></property>
    </bean>
</beans>

Person.class

package com.cmz.bean;

import java.util.Date;

/**
 * @author summer
 * @create 2020-02-28 18:43
 */
public class Person {
    private int id;
    private String name;
    private Integer age;
    private String gender;
    private Date date;

    public Person() {
    }

    public Person(int id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Person(int id, Integer age, String gender) {
        this.id = id;
        this.age = age;
        this.gender = gender;
    }

    public Person(int id, String name, Integer age, String gender, Date date) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.date = date;
    }

    public int getId() {
        return id;
    }

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

    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 getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", date=" + date +
                '}';
    }

    public void init(){
        System.out.println("Bean is going through init.");
    }
    public void destroy(){
        System.out.println("Bean will destroy now.");
    }
}

MyTest3.class

import com.cmz.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author summer
 * @create 2020-02-22 23:45
 */
public class MyTest3 {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("ioc3.xml");
        // 根据bean标签id来获取对象
        Person person = context.getBean("person", Person.class);
        System.out.println(person);
    }
}

输出

Bean is going through init.
Person{id=1, name='张三', age=20, gender='男', date=Fri Feb 28 00:00:00 CST 2020}