1. 程式人生 > >spring各種類型的屬性註入

spring各種類型的屬性註入

spring bean 多種類型屬性註入

spring test + Junit方式:


pox.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.try2better</groupId>
    <artifactId>spring</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring.version>4.3.9.RELEASE</spring.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- spring end -->
    </dependencies>
</project>

Java:

package beans;

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

/*
 * DESCRIPTION : 
 * USER : zhouhui
 * DATE : 2017/8/10 15:49
 */
public class Bean {

    private Integer inte;
    private String str;
    private String[] strArr;
    private Boss boss;
    private List<String> list;
    private Set<String> set;
    private Map<String,Boss> map;
    //Properties和Map的區別是,Properties只能接受key和value都是String
    private Properties properties;

    public Integer getInte() {
        return inte;
    }

    public void setInte(Integer inte) {
        this.inte = inte;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public String[] getStrArr() {
        return strArr;
    }

    public void setStrArr(String[] strArr) {
        this.strArr = strArr;
    }

    public Boss getBoss() {
        return boss;
    }

    public void setBoss(Boss boss) {
        this.boss = boss;
    }

    public List<String> getList() {
        return list;
    }

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

    public Set<String> getSet() {
        return set;
    }

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

    public Map<String, Boss> getMap() {
        return map;
    }

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

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "Bean{" +
                "inte=" + inte +
                ", str=‘" + str + ‘\‘‘ +
                ", strArr=" + Arrays.toString(strArr) +
                ", boss=" + boss +
                ", list=" + list +
                ", set=" + set +
                ", map=" + map +
                ", properties=" + properties +
                ‘}‘;
    }
}

實體:

package beans;

/*
 * DESCRIPTION : 
 * USER : zhouhui
 * DATE : 2017/8/10 11:19
 */
public class Boss {

    private String name;
    private Integer age;
    private Boolean isMale;

    public Boss() {
    }

    public Boss(String name, Integer age, Boolean isMale) {
        this.name = name;
        this.age = age;
        this.isMale = isMale;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Boolean getMale() {
        return isMale;
    }

    public void setMale(Boolean male) {
        isMale = male;
    }

    @Override
    public String toString() {
        return "Boss{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", isMale=" + isMale +
                ‘}‘;
    }
}

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

    <import resource="bean.xml"/>

    <bean id="boss" class="beans.Boss">
        <property name="age" value="10"/>
        <property name="male" value="false"/>
        <property name="name" value="zhouhui"/>
    </bean>

    <bean id="boss2" class="beans.Boss" scope="singleton">
        <constructor-arg name="age" value="11"/>
        <constructor-arg name="name" value="zhouhui2"/>
        <constructor-arg name="isMale" value="true"/>
    </bean>

    <bean id="boss3" class="beans.Boss">
    </bean>

    <bean id="bossService" class="impl.BossServiceImpl">
        <property name="boss" ref="boss2"></property>
    </bean>

</beans>

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

    <!-- boss對象的bean -->
    <bean id="myBoss" class="beans.Boss">
        <property name="age" value="10"/>
        <property name="male" value="false"/>
        <property name="name" value="zhouhui"/>
    </bean>

    <bean id="bean" class="beans.Bean">
        <!-- private Boss boss; -->
        <property name="boss" ref="myBoss"/>

        <!-- private Integer inte; -->
        <property name="inte" value="1"/>

        <!-- private String str; -->
        <property name="str" value="hello world"/>

        <!-- private String[] strArr; -->
        <property name="strArr">
            <list>
                <value>NO.1</value>
                <value>NO.2</value>
                <value>NO.3</value>
            </list>
        </property>

        <!-- private List<String> list; -->
        <property name="list">
            <list>
                <value>INDEX 1</value>
                <value>INDEX 2</value>
                <value>INDEX 3</value>
            </list>
        </property>

        <!-- private Map<String,Boss> map; -->
        <property name="map">
            <map>
                <entry key="entity 1" value-ref="myBoss"></entry>
                <entry key="entity 2" value-ref="myBoss"></entry>
                <entry key="entity 3" value-ref="myBoss"></entry>
            </map>
        </property>

        <!-- private Set<String> set; -->
        <property name="set">
            <set>
                <value>set 1</value>
                <value>set 2</value>
                <value>set 3</value>
            </set>
        </property>

        <!-- private Properties properties; -->
        <property name="properties">
            <props>
                <prop key="key1">value1</prop>
                <prop key="key2">value2</prop>
                <prop key="key3">value3</prop>
            </props>
        </property>
    </bean>
</beans>


測試類:

package test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

import beans.Bean;
import beans.Boss;
import business.BossService;

/*
 * DESCRIPTION :
 * USER : zhouhui
 * DATE : 2017/8/10 11:25
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:spring-content.xml" })
public class TestSpringBeans {

    @Resource(name = "boss3")
    private Boss boss;
    @Autowired
    private Boss boss2;
    @Autowired
    private Boss boss3;

    @Autowired
    private BossService bossService;
    @Autowired
    private Bean bean;

    /**
     * @Autowired 是根據bean的id,完全匹配註入
     * @Resource(name = "boss3") 指定nid匹配註入
     */
    @org.junit.Test
    public void test(){
        System.out.println("============test===============");
        System.out.println(boss.toString());
        System.out.println(boss2.toString());
        System.out.println(boss3.toString());
        //默認是單例的,如果需要是多例的需要設置scope="singleton"
        System.out.println(boss == boss2);
        System.out.println("=============test==============");
    }

    /**
     * 測試接口的註入,必須在xml種指定實現,[email protected]
     */
    @Test
    public void testInterface(){
        System.out.println("============testInterface===============");
        System.out.println(bossService.getBossEntity());
        System.out.println("============testInterface===============");
    }

    /**
     * 對數組 集合等各種類型的屬性進行註入
     */
    @Test
    public void testBean(){
        System.out.println("===========testBean================");
        System.out.println(bean);
        System.out.println("===========testBean================");
    }
}

執行結果:

============test===============
Boss{name=‘null‘, age=null, isMale=null}
Boss{name=‘zhouhui2‘, age=11, isMale=true}
Boss{name=‘null‘, age=null, isMale=null}
false
=============test==============

============testInterface===============
Boss{name=‘zhouhui2‘, age=11, isMale=true}
============testInterface===============

===========testBean================
Bean{inte=1, str=‘hello world‘, strArr=[NO.1, NO.2, NO.3], boss=Boss{name=‘zhouhui‘, age=10, isMale=false}, list=[INDEX 1, INDEX 2, INDEX 3], set=[set 1, set 2, set 3], map={entity 1=Boss{name=‘zhouhui‘, age=10, isMale=false}, entity 2=Boss{name=‘zhouhui‘, age=10, isMale=false}, entity 3=Boss{name=‘zhouhui‘, age=10, isMale=false}}, properties={key3=value3, key2=value2, key1=value1}}
===========testBean================


spring各種類型的屬性註入