七、实现依赖注入的多种方式

2018-08-29  本文已影响0人  lifeline张

四、不同数据类型的注入方式

image.png

注:
1、bean属性是在所有的spring配置文件中寻找当前属性,local是在我们的当前配合文件中寻找当前属性元素。

示例

注:
1、当有特殊字符时,比如“P&G”,这时候如果输出的话会报错,因为G是类似于关键字的一个存在。这时候可以使用CDATA元素来解决这个问题,也可以将特殊字符转换为实体引用来解决。
2、props集合这种类型的数据是配合我们的properties文件来使用的,比如我们之前学习的JDBC的时候使用的配置文件。

实体类:

package entity;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class TestEntity {
    private String specialCharacter1; // 特殊字符值1
    private String specialCharacter2; // 特殊字符值2
    private User innerBean; // JavaBean类型
    private List<String> list; // List类型
    private String[] array; // 数组类型
    private Set<String> set; // Set类型
    private Map<String, String> map; // Map类型
    private Properties props; // Properties类型
    private String emptyValue; // 注入空字符串值
    private String nullValue = "init value"; // 注入null值

    public void setSpecialCharacter1(String specialCharacter1) {
        this.specialCharacter1 = specialCharacter1;
    }

    public void setSpecialCharacter2(String specialCharacter2) {
        this.specialCharacter2 = specialCharacter2;
    }

    public void setInnerBean(User user) {
        this.innerBean = user;
    }

    public void setList(List<String> list) {
        this.list = list;
    }
    
    public void setArray(String[] array) {
        this.array = array;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public void setProps(Properties props) {
        this.props = props;
    }

    public void setEmptyValue(String emptyValue) {
        this.emptyValue = emptyValue;
    }

    public void setNullValue(String nullValue) {
        this.nullValue = nullValue;
    }

    public void showValue() {
        System.out.println("特殊字符1:" + this.specialCharacter1);
        System.out.println("特殊字符2:" + this.specialCharacter2);
        System.out.println("内部Bean:" + this.innerBean.getUsername());
        System.out.println("List属性:" + this.list);
        System.out.println("数组属性[0]:" + this.array[0]);
        System.out.println("Set属性:" + this.set);
        System.out.println("Map属性:" + this.map);
        System.out.println("Properties属性:" + this.props);
        System.out.println("注入空字符串:[" + this.emptyValue + "]");
        System.out.println("注入null值:" + this.nullValue);
    }
}
package entity;

/**
 * 用户实体类
 */
public class User implements java.io.Serializable {
    private String username; // 用户名

    // getter & setter
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

}

配置文件:

<?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-3.2.xsd">
    <bean id="testEntity" class="entity.TestEntity">
        <!-- 1 -->
        <!-- 使用CDATA处理XML文件中的特殊字符 -->
        <property name="specialCharacter1">
            <value><![CDATA[P&G]]></value>
        </property>
        <!-- 2 -->
        <!-- 将特殊字符转化为实体引用 -->
        <property name="specialCharacter2">
            <value>P&amp;G</value>
        </property>
        <!-- 3 -->
        <property name="innerBean">
            <bean class="entity.User">
                <property name="username">
                    <value>张三</value>
                </property>
            </bean>
        </property>
        <!-- 4 -->
        <!-- 配置list集合类型的值 -->
        <property name="list">
            <list>
                <value>足球</value>
                <value>篮球</value>
            </list>
        </property>
        <!-- 5 -->
        <!-- 配置数组类型的值 -->
        <property name="array">
            <list>
                <value>足球</value>
                <value>篮球</value>
            </list>
        </property>
        <!-- 6 -->
        <!-- 配置set类型的值 -->
        <property name="set">
            <set>
                <value>足球</value>
                <value>篮球</value>
            </set>
        </property>
        <!-- 7 -->
        <!-- 配置map类型的值 -->
        <property name="map">
            <map>
                <entry>
                    <key><value>football</value></key>
                    <value>足球</value>
                </entry>
                <entry>
                    <key><value>basketball</value></key>
                    <value>篮球</value>
                </entry>
            </map>
        </property>
        <!-- 8 -->
        <!-- 配置properties类型的值 -->
        <property name="props">
            <props>
                <prop key="football">足球</prop>
                <prop key="basketball">篮球</prop>
            </props>
        </property>
        <!-- 9 -->
        <!-- 空值""的处理 -->
        <property name="emptyValue">
            <value></value>
        </property>
        <!-- 10 -->
        <!-- null的处理 -->
        <property name="nullValue">
            <null></null>
        </property>
    
    
    
    
    
    
    
    </bean>
</beans>

测试类代码:

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import entity.TestEntity;

public class Test {

    @org.junit.Test
    public void test() {
        // 使用ApplicationContext接口的实现类ClassPathXmlApplicationContext加载Spring配置文件
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        TestEntity entity = (TestEntity) ctx.getBean("testEntity");
        entity.showValue();
    }

}

运行结果:


image.png

五、小结

image.png
上一篇下一篇

猜你喜欢

热点阅读