current position:Home>Spring (7): di dependency injection

Spring (7): di dependency injection

2022-09-23 10:18:27Who knows if the water is hot or not


依赖关系注入 (DI) 是一个过程,通过该过程,对象仅通过构造函数参数、Parameters to a factory method or properties that are set on an object instance after it is constructed or returned from a factory method define its dependencies(即,使

with other objects they use).然后,容器在创建 Bean 时注入这些依赖项.这个过程基本上是Beaninverse function of itself(因此得名“控制反转”),Controlled by using the direct construction of the class or the Service Locator pattern

The instantiation or location of its dependencies.

使用 DI 原则,代码更清晰,When an object is provided with its dependencies,解耦更有效.该对象不查找其依赖项,也不知道依赖项的位置或类.


依赖注入有三种方式:Constructor-based dependency injection、利用setDependency injection for methods、Additional dependency injection.

1.Constructor-based dependency injection

这个在Spring(五)It has already been said in the study,Here is only a brief description of the way of constructor injection.

(1)通过indexindex for injection,索引从0开始.

(2)Inject by passing in the parameter name,最方便.

(3)Inject by the type of the parameter passed,Does not apply to situations of the same type.


2.利用setDependency injection for methods

通过setMethod dependency injection is the core injection method.

This previous study has also been used,However, the previous injections are some simple basic types andString类型,Here we go to some complex types such as List、MapAnd so on to learn the injection method.

(1)Student和Addressis the class we are going to use


package com.jms.pojo;

public class Address {
    private int id;
    private String address;

    public int getId() {
        return id;

    public void setId(int id) { = id;

    public String getAddress() {
        return address;

    public void setAddress(String address) {
        this.address = address;

    public String toString() {
        return "Address{" +
                "id=" + id +
                ", address='" + address + '\'' +


package com.jms.pojo;

import java.util.*;

public class Student {

    private String name;
    private Address address;
    private String[] lesson;
    private List<String> hobbys;
    private Map<String, String> card;
    private Set<String> games;
    private String graduate;
    private Properties info;

    public String[] getLesson() {
        return lesson;

    public void setLesson(String[] lesson) {
        this.lesson = lesson;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public Address getAddress() {
        return address;

    public void setAddress(Address address) {
        this.address = address;

    public List<String> getHobbys() {
        return hobbys;

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;

    public Map<String, String> getCard() {
        return card;

    public void setCard(Map<String, String> card) {
        this.card = card;

    public Set<String> getGames() {
        return games;

    public void setGames(Set<String> games) { = games;

    public String getGraduate() {
        return graduate;

    public void setGraduate(String graduate) {
        this.graduate = graduate;

    public Properties getInfo() {
        return info;

    public void setInfo(Properties info) { = info;

    public String toString() {
        return "Student{" + "\n" +
                "name='" + name + '\'' + "\n" +
                "address=" + address.toString() + "\n" +
                "lesson=" + Arrays.toString(lesson) + "\n" +
                "hobbys=" + hobbys + "\n" +
                "card=" + card + "\n" +
                "games=" + games + "\n" +
                "graduate='" + graduate + '\'' + "\n" +
                "info=" + info + "\n" +


<bean id="student" class="com.jms.pojo.Student">
        <property name="name" value="jms"/>
        <property name="address" ref="address"/>
        <property name="lesson">
        <property name="hobbys">
        <property name="card">
                <entry key="学生卡" value="101010101"/>
                <entry key="饭卡" value="20202020202"/>
                <entry key="水卡" value="30303030303"/>
        <property name="games">
        <property name="graduate">
        <property name="info">
                <prop key="学号">100001</prop>
                <prop key="姓名">ah van der sar</prop>
                <prop key="班级">计算机3班</prop>

    <bean id="address" class="com.jms.pojo.Address">
        <property name="id" value="1"/>
        <property name="address" value="world"/>


    public void studentTest() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Student student = applicationContext.getBean("student", Student.class);


3.Additional dependency injection

 c命名空间注入和p命名空间注入,This is the official injection method of expansion,pNamespace injection correspondsset注入,cNamespace injection corresponds to parameterized constructor injection.

Constraints need to be added when using both injections,The following are the official usage examples:


<beans xmlns=""

    <bean name="john-classic" class="com.example.Person">
        <property name="name" value="John Doe"/>
        <property name="spouse" ref="jane"/>

    <bean name="john-modern"
        p:name="John Doe"

    <bean name="jane" class="com.example.Person">
        <property name="name" value="Jane Doe"/>


<beans xmlns=""

    <bean id="beanTwo" class="x.y.ThingTwo"/>
    <bean id="beanThree" class="x.y.ThingThree"/>

    <!-- traditional declaration with optional argument names -->
    <bean id="beanOne" class="x.y.ThingOne">
        <constructor-arg name="thingTwo" ref="beanTwo"/>
        <constructor-arg name="thingThree" ref="beanThree"/>
        <constructor-arg name="email" value="[email protected]"/>

    <!-- c-namespace declaration with argument names -->
    <bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
        c:thingThree-ref="beanThree" c:email="[email protected]"/>





copyright notice
author[Who knows if the water is hot or not],Please bring the original link to reprint, thank you.

Random recommended