6 DI 依赖注入

2022-04-11  本文已影响0人  Messix_1102

6.1 构造器注入

前面已经讲过

6.2 Set方式注入【重点】

  1. 复杂类型
package com.hunter.pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
  1. 真实测试对象
package com.hunter.pojo;

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 Properties info;
    private String wife;
    ... Get Set ToString
}

  1. beans
<?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">

    <!-- more bean definitions go here -->
    <bean id="address" class="com.hunter.pojo.Address">
        <property name="address" value="xian"></property>
    </bean>

    <bean id="student" class="com.hunter.pojo.Student">
        <!-- 第一种,普通值注入, value-->
        <property name="name" value="hunter"></property>
        <!-- 第二种,bean注入, ref-->
        <property name="address" ref="address"></property>
        <!-- 第三种,数组注入-->
        <property name="books">
            <array>
                <value>red building dream</value>
                <value>west gogogo</value>
                <value>water tiger roll</value>
                <value>three kingdoms</value>
            </array>
        </property>
        <!-- 第四种,List注入-->
        <property name="hobbys">
            <list>
                 <value>watch movie</value>
                 <value>listen music</value>
            </list>
        </property>
        <!--第五种,Map注入-->
        <property name="card">
            <map>
                 <entry key="IDCard" value="658745545645" />
                 <entry key="BankCard" value="2545861235" />
            </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="Num">201110437</prop>
                <prop key="Gender">male</prop>
                <prop key="Url">http://www.baidu.com</prop>
                <prop key="un">root</prop>
                <prop key="pwd">123456</prop>
            </props>
        </property>
    </bean>

</beans>
  1. 测试类
import com.hunter.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.toString());
        /*
        Student{
            name='hunter',
            address=Address{address='xian'},
            books=[
                red building dream,
                west gogogo,
                water tiger roll,
                three kingdoms],
            hobbys=[
                watch movie,
                listen music],
            card={
                IDCard=658745545645,
                BankCard=2545861235
                },
            games=[lol, coc, bob],
            info={
                un=root,
                Gender=male,
                pwd=123456,
                Num=201110437,
                Url=http://www.baidu.com
                },
            wife='null'}
        * */
    }
}

6.3 其他方式

我们可以使用p命名空间和c命名空间
官方解释


截图
<?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">

    <!-- more bean definitions go here -->
    <!-- P 命名空间注入, 可以直接注入属性的值-->
    <bean id="user" class="com.hunter.pojo.User" p:name="hunter" p:age="18"></bean>
    <!-- c 命名空间注入, 构造器注入-->
    <bean id="user2" class="com.hunter.pojo.User" c:name="hunter2" c:age="19"></bean>
</beans>
public class MyTest {
    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
        User user = context.getBean("user2", User.class);
        System.out.println(user.toString());
    }
}

注意点:p名面和c命名不能直接使用,需要导入约束

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

6.4 bean 的作用域

截图
<bean id="user2" class="com.hunter.pojo.User" c:name="hunter2" c:age="19" scope="singleton"></bean>
<bean id="user2" class="com.hunter.pojo.User" c:name="hunter2" c:age="19" scope="prototype"></bean>
上一篇 下一篇

猜你喜欢

热点阅读