Spring5.0 -- 通过 Set 方式依赖注入(DI)普通注入、数组、List、Map、Set、Null、Properties;使用p命名空间和c命名空间进行注入

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

一. 通过 Set 方式注入[ 重点 ]

1.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.2 Student 类

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

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

1.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" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

    <bean id="address" class="pojo.Address"/>

    <bean id="student" class="pojo.Student">
        <!-- 普通注入 -->
        <property name="name" value="kc"/>

        <!--第二种,Bean注入,ref-->
        <property name="address" ref="address"/>

        <!--数组-->
        <property name="books">
            <array>
                <value>刘师傅爱情技巧真传</value>
                <value>刘师傅教你如何一夜暴富</value>
                <value>刘师傅教你如何走上人生巅峰</value>
                <value>刘师傅成功学</value>
            </array>
        </property>

        <!--List-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
                <value>看电影</value>
            </list>
        </property>

        <!--Map-->
        <property name="card">
            <map>
                <entry key="身份证" value="1111112222223333"/>
                <entry key="银行卡" value="123123123123123123"/>
            </map>
        </property>

        <!--Set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
                <value>BOB</value>
            </set>
        </property>

        <!--null-->
        <property name="wife">
            <null/>
        </property>

        <!--Properties-->
        <property name="info">
            <props>
                <prop key="driver">20190525</prop>
                <prop key="url"></prop>
                <prop key="username">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>
    </bean>


</beans>

1.4 测试类

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

1.5 结果

  • Student{name=‘kc’, address=Address{address=‘null’},
  • books=[刘师傅爱情技巧真传, 刘师傅教你如何一夜暴富, 刘师傅教你如何走上人生巅峰, 刘师傅成功学],
  • hobbys=[听歌, 敲代码, 看电影],
  • card={身份证=1111112222223333, 银行卡=123123123123123123}, - games=[LOL, COC, BOB],
  • wife=‘null’,
  • info={password=123456, url=男, driver=20190525, username=root}}

二. 拓展空间注入(p命名空间、c命名空间)

我们可以使用p命名空间和c命名空间进行注入

2.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-->
    <bean id="user" class="com.chen.pojo.User" p:name="陈浩" p:age="18" />

    <!--c命名空间注入,通过构造器注入属性的值:construct-args-->
    <bean id="user2" class="com.chen.pojo.User" c:age="18" c:name="陈浩"/>

</beans>

2.2 测试类

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

    }

2.3 注意点

p命名和c命名空间不能直接使用,需要导入 xml 约束(Spring官方文档有)

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

猜你喜欢

转载自blog.csdn.net/Kc635908933/article/details/114178659