Spring(三)DI

(1)DI概念
利用spring IOC实例化了对象,而DI将实例化的对象注入到需要对象的地方,完成初始化任务。对象由spring创建,之后再由spring给属性赋值。
spring提供两种方式设置属性值:① setter方法注入。②构造方法注入
(2)set方法注入
Cat.java

public class Cat implements Serializable{
    
    private static final long serialVersionUID = 1L;
    
    private String type;
    private int age;
    
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
<!-- 实例化Cat对象
     <bean>节点是用来告知spring实例化对象
     <property>节点,是用来告知spring对象有注入
     name="type" type->Type->setType
     把名字的第一个字母大写,前面用字符串连接一个set,构建出一个新字符串,
     拿着个字符串去class属性所对应的类中寻找是否有此方法签名的方法.
     如果有就反射调用此setter方法.setter方法的参数是value="smallCat"
-->
<bean id="cat" class="com.hdu.setter.Cat">
    <!-- 单值注入 -->
    <property name="type" value="smallCat"></property>
    <property name="age" value="3"></property>
</bean>

测试

public class TestIOCDI {
    @Test
    public void testMethod() {
        ApplicationContext context = new ClassPathXmlApplicationContext("resources/spring_setter_object.xml");
        Cat cat = context.getBean("cat",Cat.class);
        System.out.println(cat.getType()+" "+cat.getAge());
    }
}

(3)对象注入
Person.java

public class Person {
    private String name;
    private int age;
    private Cat cat;
    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;
    }
    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }    
}
<!-- 实例化Cat对象 -->
<bean id="cat" class="com.hdu.setter.Cat">
    <!-- 单值注入 -->
    <property name="type" value="smallCat"></property>
    <property name="age" value="3"></property>
</bean>
<!-- name="cat" cat->Cat->setCat
     ref="cat" 引用对象,cat是spring容器中的唯一id,指上面已经实例化的cat对象
-->
<bean id="person" class="com.hdu.setter.Person">
<!-- 单值注入 -->
<property name="name" value="kuotian"></property>
<property name="age" value="23"></property>
<!-- 对象注入 -->
<property name="cat" ref="cat"></property>
</bean>

测试

public class TestIOCDI {
    @Test
    public void testMethod() {
        ApplicationContext context = new ClassPathXmlApplicationContext("resources/spring_setter_object.xml");
        //对象都是由spring创建和管理,对象的关系也要由spring来维护
        Person person = context.getBean("person",Person.class);
        System.out.println("Person:name="+person.getName());
        System.out.println("Person:age="+person.getAge());
        System.out.println("Person:Cat:type="+person.getCat().getType());
        System.out.println("Person:Cat:age="+person.getCat().getAge());
    }
}

(4)集合注入
Message.java

public class Message {
    private List list;
    private Set set;
    private Map map;
    private Properties pros;
    public void setList(List list) {
        this.list = list;
    }
    public void setSet(Set set) {
        this.set = set;
    }
    public void setMap(Map map) {
        this.map = map;
    }
    public void setPros(Properties pros) {
        this.pros = pros;
    }
    @Override
    public String toString() {
        return "Message [list=" + list + ", set=" + set + ", map=" + map + ", pros=" + pros + "]";
    }
}

1、直接注入

<!-- 实例化Cat对象 -->
<bean id="cat" class="com.hdu.setter.Cat">
    <!-- 单值注入 -->
    <property name="type" value="smallCat"></property>
    <property name="age" value="3"></property>
</bean>
    
<!-- 直接集合注入 -->
<bean id="message" class="com.hdu.setter.Message">
    <property name="list">
        <list>
            <!-- value引用的是单值 ref引用的事对象 -->
            <value>杭州</value>
            <value>17</value>
            <ref bean="cat" />
        </list>
    </property>
    <property name="set">
        <set>
            <value>杭州</value>
            <value>17</value>
            <ref bean="cat" />
        </set>
    </property>
    <property name="map">
        <map>
            <entry key="bj" value="北京"></entry>
            <entry key="hz" value="杭州"></entry>
            <entry key="cc" value-ref="cat"></entry>
        </map>
    </property>
    <property name="pros">
        <props>
            <prop key="bj">北京</prop>
            <prop key="hz">杭州</prop>
            <prop key="sh">上海</prop>
        </props>
    </property>
</bean>

测试

public class TestIOCDI {
    @Test
    public void testMethod() {
        ApplicationContext context = new ClassPathXmlApplicationContext("resources/spring_setter_collection.xml");
        //直接注入
        Message msg = context.getBean("message",Message.class);
        System.out.println(msg);
    }
}

2、间接注入
间接注入,集合可复用多次。beans需要引入util命名空间。

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 实例化Cat对象 -->
    <bean id="cat" class="com.hdu.setter.Cat">
        <!-- 单值注入 -->
        <property name="type" value="smallCat"></property>
        <property name="age" value="3"></property>
    </bean>
    <!-- 间接集合注入 -->
    <util:list id="list">
        <value>杭州</value>
        <value>17</value>
        <ref bean="cat" />
    </util:list>
    
    <util:set id="set">
        <value>杭州</value>
        <value>17</value>
        <ref bean="cat" />
    </util:set>
    
    <util:map id="map">
        <entry key="bj" value="北京"></entry>
        <entry key="hz" value="杭州"></entry>
        <entry key="cc" value-ref="cat"></entry>
    </util:map>
    
    <util:properties id="props">
        <prop key="bj">北京</prop>
        <prop key="hz">杭州</prop>
        <prop key="sh">上海</prop>
    </util:properties>
    
    <bean id="message1" class="com.hdu.setter.Message">
        <property name="list" ref="list"></property>
        <property name="set" ref="set"></property>
        <property name="map" ref="map"></property>
        <property name="pros" ref="props"></property>
    </bean>
</beans>

测试

public class TestIOCDI {
    @Test
    public void testMethod() {
        ApplicationContext context = new ClassPathXmlApplicationContext("resources/spring_setter_collection.xml");
        //直接注入
        Message msg = context.getBean("message",Message.class);
        System.out.println("直接注入:"+msg);
        //间接注入
        Message msg1 = context.getBean("message1",Message.class);
        System.out.println("间接注入:"+msg1);
    }
}

(5)表达式注入
1、${}表达式
db.properties文件

jdbc_driverClass=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/tmpdb
jdbc_userName=root
jdbc_userPassword=root

JDBCUtil

public class JDBCUtil {
    private String driverClass;
    private String url;
    private String userName;
    private String password;
    public String getDriverClass() {
        return driverClass;
    }
    public void setDriverClass(String driverClass) {
        this.driverClass = driverClass;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "JDBCUtil [driverClass=" + driverClass + ", url=" + url + ", userName=" + userName + ", password="
                + password + "]";
    }
}

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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
    <!-- 优先加载 先把属性文件加载到spring容器中 多个文件用逗号间隔 -->
    <context:property-placeholder location="classpath:resources/mysql.properties,resources/page.properties"/>
    <!-- 表达式注入 ${} -->
    <bean id="jdbcUtil" class="com.hdu.util.JDBCUtil">
        <property name="driverClass" value="${jdbc_driverClass}"></property>
        <property name="url" value="${jdbc_url}"></property>
        <property name="userName" value="${jdbc_userName}"></property>
        <property name="password" value="${jdbc_userPassword}"></property>
    </bean>
</beans>

TestIOCDI

public class TestIOCDI {
    @Test
    public void testMethod() {
        ApplicationContext context = new ClassPathXmlApplicationContext("resources/spring_setter_expression1.xml");
        //从容器中取出对象
        JDBCUtil jdbcUtil = context.getBean("jdbcUtil", JDBCUtil.class);
        System.out.println(jdbcUtil);
    }
}

2、#{}表达式
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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
    <!-- 优先加载 先把属性文件加载到spring容器中 多个文件用逗号间隔 -->
    <util:properties id="manyProperties" location="classpath:resources/mysql.properties,resources/page.properties"></util:properties>
    <!-- 表达式注入 #{} -->
    <bean id="jdbcUtil" class="com.hdu.util.JDBCUtil">
        <property name="driverClass" value="#{manyProperties.jdbc_driverClass}"></property>
        <property name="url" value="#{manyProperties.jdbc_url}"></property>
        <property name="userName" value="#{manyProperties.jdbc_userName}"></property>
        <property name="password" value="#{manyProperties.jdbc_userPassword}"></property>
    </bean>
    
</beans>

TestIOCDI

public class TestIOCDI {
    @Test
    public void testMethod() {
        ApplicationContext context = new ClassPathXmlApplicationContext("resources/spring_setter_expression2.xml");
        JDBCUtil jdbcUtil = context.getBean("jdbcUtil", JDBCUtil.class);
        System.out.println(jdbcUtil);
    }
}

(6)空值注入

public class Kong {
    private String str1;
    private String str2;
    public String getStr1() {
        return str1;
    }
    public void setStr1(String str1) {
        this.str1 = str1;
    }
    public String getStr2() {
        return str2;
    }
    public void setStr2(String str2) {
        this.str2 = str2;
    }
    @Override
    public String toString() {
        return "Kong [str1=" + str1 + ", str2=" + str2 + "]";
    }
}

<bean id="kong" class="com.hdu.util.Kong">
    <property name="str1" value=""></property>
    <property name="str2">
        <null></null>
    </property>
</bean>

public class TestIOCDI {
    @Test
    public void testMethod() {
        ApplicationContext context = new ClassPathXmlApplicationContext("resources/spring_setter_kong.xml");
        Kong kong = context.getBean("kong", Kong.class);
        System.out.println(kong);
    }
}

(7)构造方法注入
ConstructorDI

public class ConstructorDI {
    private int age;
    private Cat cat;
    private String name;
    public ConstructorDI() {
        
    }
    
    public ConstructorDI(int age, Cat cat, String name) {
        super();
        this.age = age;
        this.cat = cat;
        this.name = name;
    }
    
    @Override
    public String toString() {
        return "ConstructorDI [age=" + age + ", cat=" + cat + ", name=" + name + "]";
    }
}

xml配置文件

<!-- 实例化Cat对象 -->
<bean id="cat" class="com.hdu.setter.Cat">
    <property name="type" value="smallCat"></property>
    <property name="age" value="3"></property>
</bean>
<!-- 
     index:从0开始,代表参数的位置
 -->
<bean id="constructor" class="com.hdu.constructor.ConstructorDI">
    <constructor-arg index="0" value="20"></constructor-arg>
    <constructor-arg index="1" ref="cat"></constructor-arg>
    <constructor-arg index="2" value="张三"></constructor-arg>
</bean>

TestIOCDI

public class TestIOCDI {
    @Test
    public void testMethod() {
        ApplicationContext context = new ClassPathXmlApplicationContext("resources/spring_constructor.xml");
        ConstructorDI constructor = context.getBean("constructor", ConstructorDI.class);
        System.out.println(constructor);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值