从零开始学习Spring5架构(五)——依赖注入(DI)

依赖注入(Dependency Injection,DI)

  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源

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

构造器注入

有参构造器注入,前文有具体的讲解,此处给出代码实例

<!-- 第一种根据index参数下标设置 -->
    <bean id="user" class="com.pag.pojo.User">
    <!-- index指构造方法 , 下标从0开始 -->
        <constructor-arg index="0" value="pag"/>
    </bean>

<!-- 第二种根据参数名字设置 -->
    <bean id="user" class="com.pag.pojo.User">
    <!-- name指参数名 -->
        <constructor-arg name="name" value="pag"/>
    </bean>

<!-- 第三种根据参数类型设置 -->
    <bean id="user" class="com.pag.pojo.User">
        <constructor-arg type="java.lang.String" value="pag"/>
    </bean>

set注入(重点)

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

package com.pag.pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

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

Student.java

package com.pag.pojo;

import java.util.List;
import java.util.*;

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;
    }

    public void show(){
        System.out.println("name="+ name
                + ",address="+ address.getAddress()
                + ",books="
        );
        for (String book:books){
            System.out.print("<<"+book+">>\t");
        }
        System.out.println("\n爱好:"+hobbys);
        System.out.println("card:"+card);
        System.out.println("games:"+games);
        System.out.println("wife:"+wife);
        System.out.println("info:"+info);
    }

}

常量注入

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

测试类:

    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.getName());
    }

Bean注入

    <bean id="addr" class="com.pag.pojo.Address">
        <property name="address" value="深圳"/>
    </bean>
    <bean id="student" class="com.pag.pojo.Student">
        <property name="address" ref="addr"/>
    </bean>

测试类:

    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.getAddress().getAddress());
    }

数组注入

 <bean id="student" class="com.pag.pojo.Student">
        <property name="books">
            <array>
                <value>spring</value>
                <value>java</value>
                <value>c</value>
            </array>
        </property>
    </bean>

测试类:

    @Test
    public void test3(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        for (String s : student.getBooks()) {
            System.out.println(s);
        }
    }

List注入

    <bean id="student" class="com.pag.pojo.Student">
        <property name="hobbys">
            <list>
                <value>sing song</value>
                <value>read book</value>
            </list>
        </property>
    </bean>

测试类:

    @Test
    public void test4(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        for (String s : student.getHobbys()) {
            System.out.println(s);
        }
    }

Map注入

    <bean id="student" class="com.pag.pojo.Student">
        <property name="card">
            <map>
                <entry key="中国邮政" value="1221"/>
                <entry key="建设" value="1222"/>
            </map>
        </property>
    </bean>

测试类:

    @Test
    public void test5(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.getCard());
    }

Set注入

    <bean id="student" class="com.pag.pojo.Student">
        <property name="games">
            <set>
                <value>LOL</value>
                <value>DND</value>
                <value>COD</value>
            </set>
        </property>
    </bean>

测试类:

    @Test
    public void test6(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.getGames());
    }

Null注入

    <bean id="student" class="com.pag.pojo.Student">
        <property name="wife"><null></null></property>
    </bean>

测试类:

    @Test
    public void test7(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.getWife());
    }

Properties注入

    <bean id="student" class="com.pag.pojo.Student">
        <property name="info">
            <props>
                <prop key="学号">20210101</prop>
                <prop key="姓名">林而异</prop>
                <prop key="性别"></prop>
            </props>
        </property>
    </bean>

测试类:

    @Test
    public void test8(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.getInfo());
    }

拓展注入实现

创建一个User实体类:

package com.pag.pojo;

public class User {
    private String name;
    private int age;

     public User() {
        System.out.println("无参");
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("有参");
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

p命名空间注入

需要在头文件中假如约束文件

 xmlns:p="http://www.springframework.org/schema/p"
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.pag.pojo.User" p:name="pag" p:age="21"/>

测试类:

    @Test
    public void test10(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user.toString());
    }

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

结果证明,p命名空间注入调用的是无参构造方法,使用set方法将参数传输到对象中

c命名空间注入

 xmlns:c="http://www.springframework.org/schema/c"
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.pag.pojo.User" c:name="pag" c:age="21"/>

运行结果:
在这里插入图片描述
结果证明,c命名空间注入调用的是有参构造方法,c 就是所谓的构造器注入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FFFPAG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值