Spring DI依赖注入方式

33 篇文章 0 订阅
13 篇文章 0 订阅

1.构造器注入

2.Set方式注入【重点】

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

【环境搭建】
Student

package com.lx.pojo;

import java.util.*;

/**
 * @author LongXi
 * @create 2021-05-27 20:49
 */
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobby;
    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> getHobby() {
        return hobby;
    }

    public void setHobby(List<String> hobby) {
        this.hobby = hobby;
    }

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

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

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

address

package com.lx.pojo;

/**
 * @author LongXi
 * @create 2021-05-27 20:49
 */
public class Address {
    private String address;

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

    public String getAddress() {
        return address;
    }

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

bean.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="address" class="com.lx.pojo.Address">
        <property name="address" value="长春"></property>
     </bean>


    <bean id="user" class="com.lx.pojo.Student" name="student">
        <!--第一种:普通值注入 value-->
        <property name="name" value="龙兮"/>
        <!--        <property name="name">-->
        <!--            <value>龙兮 </value>-->
        <!--        </property>-->

        <!--第二种:bean注入 ref-->
        <property name="address" ref="address"/>
        <!--数组-->
        <property name="books" >
            <array>
                <value>三国</value>
                <value>水浒</value>
            </array>
        </property>
        <!--list-->
        <property name="hobby">
            <list>
                <value>听歌</value>
                <value>写作</value>
                <value>看电影</value>
            </list>
        </property>
        <!--map-->
        <property name="card">
            <map>
                <entry key="身份证" value="2202841998"></entry>
                <entry key="银行卡" value="2544879879878"></entry>
            </map>
        </property>
        <!--set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>真三</value>
            </set>
        </property>
        <!--null-->
        <property name="wife" value=""/>
        <!--        <property name="wife">-->
        <!--            <null/>-->
        <!--        </property>-->
        <!--property-->
        <property name="info">
            <props >
                <prop key="学号:">32</prop>
                <prop key="年龄:">18</prop>
            </props>
        </property>
    </bean>



</beans>

测试类


/**
 * @author LongXi
 * @create 2021-05-27 21:24
 */
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Student student = (Student) context.getBean("student");
        System.out.println("student:"+student.toString());

    }
}

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

3.拓展方式注入

1.p命名空间

导入约束

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <!--P命名空间注入,可以直接注入属性值:property-->
        <bean id="user" class="com.lx.pojo.User" p:name="龙兮" p:age="18"/>
</beans>

实体类

package com.lx.pojo;

/**
 * @author LongXi
 * @create 2021-05-27 22:52
 */
public class User {
    private String name;
    private int age;

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

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

测试类

/**
 * @author LongXi
 * @create 2021-05-27 22:57
 */

public class MyTest2 {
    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userBean.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user);

    }
}

结果
在这里插入图片描述
2.c命名空间注入
User bean里加入无/有构造方法

    public User() {
    }

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

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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

        <!--P命名空间注入,可以直接注入属性值:property-->
        <bean id="user" class="com.lx.pojo.User" p:name="龙兮" p:age="18"/>

        <!--C命名空间注入,通过构造器注入:constroctor-->
        <bean id="user2" class="com.lx.pojo.User" c:age="19" c:name="龙兮2"/>

</beans>

测试类

    @Test
    public void test3(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userBean.xml");
        User user = context.getBean("user2", User.class);
        System.out.println(user);

    }

结果
在这里插入图片描述

注意点:
P命名和C命名不能直接使用,需要导入XML约束。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值