Spring依赖注入(DI)

依赖注入的解读:
依赖:bean对象的创建依赖于容器;
注入:bean对象中的所有属性,由容器来注入。

注入的三种方式

构造器注入(和之前的一样);Set方式注入;拓展方式注入

Set方式注入(重点)

基本步骤

1.编写复杂类型
2.编写真实测试对象
3.编写beans.xml
4.测试

1.复杂类型

public class Address {
    
    
    private String address;

    public String getAddress() {
    
    
        return address;
    }

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

    @Override
    public String toString() {
    
    
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}

2.真实测试对象

public class Student {
    
    
    public String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Set<String> game;
    private Map<String,String> card;
    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 Set<String> getGame() {
    
    
        return game;
    }

    public void setGame(Set<String> game) {
    
    
        this.game = game;
    }

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

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

    public Properties getInfo() {
    
    
        return info;
    }

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

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

3.bean.xml

数组注入

 <property name="books">
     <array>
         <value>Spring企业级开发</value>
         <value>Java开发手册</value>
         <value>码出高效</value>
     </array>
 </property>

List注入

 <property name="hobbys">
     <list>
         <value>看电影</value>
         <value>听音乐</value>
         <value>篮球</value>
     </list>
 </property>

Set注入

<property name="game">
    <set>
        <value>LOL</value>
        <value>LOLM</value>
        <value>and so on...</value>
    </set>
</property>

Map注入

<property name="card">
    <map>
        <entry key="QQ" value="123456789"/>
        <entry key="微信" value="123456789"/>
    </map>
</property>

Properties注入

<property name="info">
    <props>
        <prop key="学号">20211101</prop>
        <prop key="性别"></prop>
        <prop key="成绩"></prop>
    </props>
</property>

整合

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.feng.pojo.Address">
        <property name="address" value="成都"/>
    </bean>

    <bean id="student" class="com.feng.pojo.Student">
        <property name="name" value="小风"/>
        <property name="address" ref="address"/>
        <property name="books">
            <array>
                <value>Spring企业级开发</value>
                <value>Java开发手册</value>
                <value>码出高效</value>
            </array>
        </property>
        <property name="hobbys">
            <list>
                <value>看电影</value>
                <value>听音乐</value>
                <value>篮球</value>
            </list>
        </property>
        <property name="game">
            <set>
                <value>LOL</value>
                <value>LOLM</value>
                <value>and so on...</value>
            </set>
        </property>
        <property name="card">
            <map>
                <entry key="QQ" value="123456789"/>
                <entry key="微信" value="123456789"/>
            </map>
        </property>
        <property name="info">
            <props>
                <prop key="学号">20211101</prop>
                <prop key="性别"></prop>
                <prop key="成绩"></prop>
            </props>
        </property>
    </bean>
    

</beans>

4.测试

public class MyTest {
    
    

    @Test
    public void test01(){
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = context.getBean("student", Student.class);

        System.out.println(student.toString());
    }
}

结果为:
在这里插入图片描述

拓张方式注入(P,C命名空间)

使用p命名注入,可以直接注入属性的值:property
使用步骤:1.导入xml约束;2.p:name="name"的形式就行赋值

<!--p命名空间的xml约束-->
 xmlns:p="http://www.springframework.org/schema/p"

<!--使用-->
<bean id="user" class="com.feng.pojo.User" p:name="小风" p:age="18"/>

使用c命名注入,可通过有参构造进行注入:constructs-args
使用步骤:1.导入xml约束;2.c:name="name"的形式就行赋值

<!--p命名空间的xml约束-->
 xmlns:c="http://www.springframework.org/schema/c"

<!--使用-->
<bean id="user" class="com.feng.pojo.User" c:name="小风" c:age="18"/>

猜你喜欢

转载自blog.csdn.net/shuati2000/article/details/121092769