依赖注入的三种方式

1. 构造器注入(前边已讲)

2. Set方式注入【重点】

2.1 依赖注入含义

  1. 依赖:bean对象的创建依赖于容器
  2. 注入:bean对象的所有属性,由容器来注入

2.2 环境搭建

  1. 复杂类型
    创建引用类型Address
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 + '\'' +
                '}';
    }
}
  1. 真实测试对象
    创建类Student
package com.kuang.pojo;

import lombok.Data;
import java.util.*;

@Data
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;/*配置类*/

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

  1. beans.xml
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
       
    <bean id="student" class="com.kuang.pojo.Student">
        <!--String类型的注入: 使用value-->
        <property name="name" value="星时"></property>
    </bean>
    
</beans>
  1. 测试类
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.getName());
    }
}

2.3 完善注入

  1. 注入
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.kuang.pojo.Address">
        <property name="address" value="东胜神洲花果山水帘洞"></property>
    </bean>

    <bean id="student" class="com.kuang.pojo.Student">
        <!--String注入: 使用value-->
        <property name="name" value="星时"></property>
        <!--bean注入: 使用ref-->
        <property name="address" ref="address"></property>
        <!--数组注入-->
        <property name="books">
            <array>
                <value>杀死一只知更鸟</value>
                <value>醒来觉得甚是爱你</value>
                <value>明朝那些事儿</value>
            </array>
        </property>
        <!--list注入-->
        <property name="hobbies">
            <list>
                <value>象棋</value>
                <value>羽毛球</value>
                <value>跑步</value>
            </list>
        </property>
        <!--map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="2394872394382472935482"></entry>
                <entry key="银行卡" value="4357293854729384752398"></entry>
                <entry key="饭卡" value="294723945735732854548557"></entry>
            </map>
        </property>
        <!--Set注入-->
        <property name="games">
            <set>
                <value>王者荣耀</value>
                <value>和平精英</value>
                <value>红色警戒</value>
            </set>
        </property>
        <!--null注入-->
        <property name="wife">
            <null/>
        </property>
        <!--properties(配置)注入-->
        <property name="info">
            <props>
                <prop key="姓名">星时</prop>
                <prop key="年龄">232</prop>
                <prop key="智商">250</prop>
                <prop key="email">23424729@qq.com</prop>
            </props>
        </property>
    </bean>
</beans>
  1. 测试
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());

        /*运行结果:
        * Student{
        * name='星时',
        * address=Address{address='东胜神洲花果山水帘洞'},
        * books=[杀死一只知更鸟, 醒来觉得甚是爱你, 明朝那些事儿],
        * hobbys=[象棋, 羽毛球, 跑步],
        * card={
        *       身份证=2394872394382472935482,
        *       银行卡=4357293854729384752398,
        *       饭卡=294723945735732854548557
        *       },
        * games=[王者荣耀, 和平精英, 红色警戒],
        * wife='null',
        * info={
        *       智商=250,
        *       email=23424729@qq.com,
        *       姓名=星时,
        *       年龄=232
        *      }
        * }
        * */
    }
}

3. 拓展方式注入

3.1 p命名空间注入——主要针对无参构造方法

  1. User类
@Data
public class User {
    private String name;
    private int age;

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
  1. userBean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
        xmlns:p="http://www.springframework.org/schema/p":导入p命名空间
        xmlns:c="http://www.springframework.org/schema/c":导入c命名空间
    -->
    <!--p命名空间注入,可以直接注入属性的值: property-->
    <bean id="user" class="com.kuang.pojo.User" p:name="阿飞" p:age="14"/>
</beans>
  1. 测试方法
@Test
public void test(){
    ApplicationContext context=new ClassPathXmlApplicationContext("userBean.xml");
    User user = context.getBean("user", User.class);/*第二个参数表示的是user的类型*/
    System.out.println(user.toString());
}

<!--
    运行结果:
            User{name='阿飞', age=14}
-->

3.2 c命名空间注入——主要针对有参构造方法

  1. User类(相比上边多了两个构造方法)
@Data
public class User {
    private String name;
    private int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
  1. userBean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
        xmlns:p="http://www.springframework.org/schema/p":导入p命名空间
        xmlns:c="http://www.springframework.org/schema/c":导入c命名空间
    -->
    <!--p命名空间注入,可以直接注入属性的值: property-->
    <bean id="user" class="com.kuang.pojo.User" p:name="阿飞" p:age="14"/>

    <!--c命名空间注入,通过构造器注入: constructs-args-->
    <bean id="user2" class="com.kuang.pojo.User" c:name="星时" c:age="123"/>
</beans>
  1. 测试方法
@Test
public void test(){
    ApplicationContext context=new ClassPathXmlApplicationContext("userBean.xml");
    User user = context.getBean("user2", User.class);/*第二个参数表示的是user的类型*/
    System.out.println(user.toString());
}

<!--
    运行结果:
            User{name='星时', age=123}
-->

注意:使用p命名空间和c命名空间需要导入约束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值