Spring5依赖注入(DI)Set方式注入收录

发布于:2024-06-18 ⋅ 阅读:(22) ⋅ 点赞:(0)

Spring5依赖注入(DI)Set方式注入收录

依赖注入(Dependency Injection,DI)。

依赖 : 指Bean对象的创建依赖于容器,Bean对象的依赖资源。

注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配。

Set方式注入

要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型, 没有set方法 , 则为 is .

pojo文件下构建实体类

Address.java

package com.lanyy.pojo;

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 + '\'' +
                '}';
    }
}

Student.java

package com.lanyy.pojo;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    // 数组
    private String[] books;
    // List集合
    private List<String> hobbies;
    // 键值对
    private Map<String,String> card;
    // Set集合
    private Set<String> games;
    private String wife;

    private Properties info;

//    public Student(String name) {
//        this.name = name;
//    }
//
//    public Student() {
//
//    }

    public String getName() {
        return name;
    }
    // setter注入
    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> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    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.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

1.1 各类注入方法

注:这里的值是一个引用,ref

<?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">
<!--  引入address实体类  -->
    <bean id="address" class="com.lanyy.pojo.Address">
            <property name="address" value="北京"/>
    </bean>

    <bean id="student" class="com.lanyy.pojo.Student">
        <!--   第一种 常量注入 value     -->
        <property name="name" value="kyh"/>
        <!--   第一种 bean注入 ref引入实体类Address     -->
        <property name="address" ref="address"/>
        <!--  数组注入  -->
        <property name="books">
            <array>
                <value>西游记</value>
                <value>水浒传</value>
                <value>红楼梦</value>
            </array>
        </property>
        <!--   List注入   -->
        <property name="hobbies">
            <list>
                <value>lanyy</value>
                <value>kkkkk</value>
                <value>lan66</value>
            </list>
        </property>
        <!--  Map键值对注入   -->
        <property name="card">
            <map>
                <entry key="kang" value="123456789"/>
                <entry key="lanyy" value="99999999"/>
                <entry key="kkkkk" value="88888888"/>
            </map>
        </property>
        <!--  集合注入   -->
        <property name="games">
            <set>
                <value>CF</value>
                <value>CS</value>
                <value>LOL</value>
            </set>
        </property>
        <!--  空字符串属性为空 或者 引入null标签 -->
<!--        <property name="wife" value=""/>-->
        <property name="wife">
            <null/>
        </property>
        <!--  特殊类型注入 键值 注意与map的键值区分不同
             key=value
        -->
        <property name="info">
            <props>
                <prop key="cards">19311080123</prop>
                <prop key="name">懒羊羊</prop>
                <prop key="sex"></prop>
            </props>
        </property>
    </bean>
</beans>

Test类编写

import com.lanyy.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        // 只打印 Student实体类中getName()下的name值
//        System.out.println(student.getName());
        System.out.println(student.toString());
        /*
         * Student{name='kyh', address=Address{address='北京'},
         * books=[西游记, 水浒传, 红楼梦], hobbies=[lanyy, kkkkk, lan66],
         * card={kang=123456789, lanyy=99999999, kkkkk=88888888},
         * games=[CF, CS, LOL], wife='null',
         * info={name=懒羊羊, sex=男, cards=19311080123}}
         * */
    }
}

1.2 运行结果


 hobbies=[lanyy, kkkkk, lan66],
         * card={kang=123456789, lanyy=99999999, kkkkk=88888888},
         * games=[CF, CS, LOL], wife='null',
         * info={name=懒羊羊, sex=男, cards=19311080123}}
         * */
    }
}

在这里插入图片描述

了解更多知识请戳下:

@Author:懒羊羊