依赖注入(DI)

Posted 小布丁value

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了依赖注入(DI)相关的知识,希望对你有一定的参考价值。

依赖注入(DI)


package com.kuang.pojo;

import java.util.*;

/**
 * @description:
 * @author: Ada
 * @time: 2022/3/27
 */
public class Student 
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public Address getAddress() 
        return address;
    

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

    public String[] getBooks() 
        return books;
    

    public void setBooks(String[] books) 
        this.books = books;
    

    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) 
        this.games = games;
    

    public String getWife() 
        return wife;
    

    public void setWife(String wife) 
        this.wife = wife;
    

    public Properties getInfo() 
        return info;
    

    public void setInfo(Properties info) 
        this.info = info;
    

    @Override
    public String toString() 
        return "Student" +
                "name='" + name + '\\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\\'' +
                ", info=" + info +
                '';
    


常量注入

<bean id="student" class="com.kuang.pojo.Student">
     <property name="name" value="小明"/>
 </bean>

2、Bean注入

<bean id="addr" class="com.kuang.pojo.Address">
     <property name="address" value="重庆"/>
 </bean>
 
 <bean id="student" class="com.kuang.pojo.Student">
     <property name="name" value="小明"/>
     <property name="address" ref="addr"/>
 </bean>

3、数组注入

<bean id="student" class="com.kuang.pojo.Student">
     <property name="name" value="小明"/>
     <property name="address" ref="addr"/>
     <property name="books">
         <array>
             <value>西游记</value>
             <value>红楼梦</value>
             <value>水浒传</value>
         </array>
     </property>
 </bean>

4、List注入

 <property name="hobbys">
     <list>
         <value>听歌</value>
         <value>看电影</value>
         <value>爬山</value>
     </list>
 </property>

5、Map注入

 <property name="card">
     <map>
         <entry key="中国邮政" value="456456456465456"/>
         <entry key="建设" value="1456682255511"/>
     </map>
 </property

6、set注入

 <property name="games">
     <set>
         <value>LOL</value>
         <value>BOB</value>
         <value>COC</value>
     </set>
 </property>

7、Null注入

 <property name="wife"><null/></property>

8、Properties注入

 <property name="info">
     <props>
         <prop key="学号">20190604</prop>
         <prop key="性别"></prop>
         <prop key="姓名">小明</prop>
     </props>
 </property>

以上是关于依赖注入(DI)的主要内容,如果未能解决你的问题,请参考以下文章

一文搞懂│php 中的 DI 依赖注入

Spring5依赖注入(DI)

DI依赖注入

Spring-IOC学习笔记-03DI依赖注入

软件设计——依赖倒置

大话DI依赖注入+IOC控制反转 之 定义