Spring-依赖注入

前端之家收集整理的这篇文章主要介绍了Spring-依赖注入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

依赖注入

DI(Dependence Injection)依赖注入

1. 构造器注入

有无参构造器:property

用有参构造器:constructor-arg,三种方式

2. Set方式注入

1. 依赖注入: Set注入

  • 依赖: bean对象的创建依赖于容器!
  • 注入: bean对象中的所有属性,由容器来注入!

2. 环境搭建

1. 复杂类型

package com.wang.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 + '\'' +
                '}';
    }
}

2. 真实测试对象

package com.wang.pojo;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] book;
    private List<String> hobbies;
    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[] getBook() {
        return book;
    }

    public void setBook(String[] book) {
        this.book = book;
    }

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

3. 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="address" class="com.wang.pojo.Address">
        <property name="address" value="南京"/>
    </bean>

    <bean id="student" class="com.wang.pojo.Student">
        <!--第一种,普通值注入(基本类型),value-->
        <property name="name" value="wang sky"/>

        <!--第二种,Bean注入(自定义类型),ref,传入的值在xml中对应的id的bean中指定,不指定则为null-->
        <property name="address" ref="address"/>

        <!--数组注入-->
        <property name="book">
            <array>
                <value>红楼梦</value>
                <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="11111122222222333333"/>
                <entry key="银行卡" value="123456871313545343513"/>
            </map>
        </property>

        <!--Set注入-->
        <property name="games">
            <set>
                <value>WOW</value>
                <value>M&amp;B2</value>
                <value>FF14</value>
            </set>
        </property>

        <!--空值注入-->
<!--        <property name="wife" value=""/>-->

        <!--Null注入-->
        <property name="wife">
            <null/>
        </property>

        <!--properties注入(K-V对)-->
        <property name="info">
            <props>
                <prop key="学号">20201123</prop>
                <prop key="性别">男性</prop>
                <prop key="别名">小明</prop>
            </props>
        </property>


    </bean>

</beans>

4. 测试类

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

public class MyTest {
    @Test
    public void SpringTest() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }
}

3. 拓展方式注入

我们可以使用P命名空间和C命名空间进行注入

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"

       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">

    <!--P命名空间注入,可以直接注入属性的值: property,要先在上面导入xmlns:p="http://www.springframework.org/schema/p"-->
    <bean id="user" class="com.wang.pojo.User" p:name="wang sky" p:age="18"/>

    <!--C命名空间注入,可以通过构造器注入: constructor,但要求bean中有构造器! 要先在上面导入xmlns:c="http://www.springframework.org/schema/c"-->
    <bean id="user2" class="com.wang.pojo.User" c:age="18" c:name="wang sky"/>

</beans>

2. 测试

@Test
public void SpringTest2() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
    //可以向getBean的第二个参数利用反射传入User的类型,这样就不用再进行类型转换
    User user = context.getBean("user",User.class);
    System.out.println(user.toString());
}

@Test
public void SpringTest3() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
    //可以向getBean的第二个参数利用反射传入User的类型,这样就不用再进行类型转换
    User user = context.getBean("user2",User.class);
    System.out.println(user.toString());
}

3. 注意点

P命名和C命名空间不能直接使用,需要导入xml约束

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

猜你在找的Spring相关文章