004. Spring 注入參數

1、創建Java項目:File -> New -> Java Project

2、引入必要jar包,項目結構如下
這裏寫圖片描述

3、創建Animal實體類Animal.java

package com.spring.model;

public class Animal {

    private String species = null; // 種類

    public Animal() {
        super();
    }

    public Animal(String species) {
        super();
        this.species = species;
    }

    public String getSpecies() {
        return species;
    }

    public void setSpecies(String species) {
        this.species = species;
    }

    @Override
    public String toString() {
        return "Animal [species=" + species + "]";
    }

}

4、創建People實體類People.java

package com.spring.model;

public class People {

    private int id;
    private String name;
    private Animal animal = null;

    public People() {
        super();
    }

    public People(int id, String name, Animal animal) {
        super();
        this.id = id;
        this.name = name;
        this.animal = animal;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Animal getAnimal() {
        return animal;
    }

    public void setAnimal(Animal animal) {
        this.animal = animal;
    }

    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + ", animal=" + animal + "]";
    }

}

5、創建AnimalOwner實體類AnimalOwner.java

package com.spring.model;

public class AnimalOwner {

    private int id;
    private String name;
    private Animal animal = new Animal();

    public AnimalOwner() {
        super();
    }

    public AnimalOwner(int id, String name, Animal animal) {
        super();
        this.id = id;
        this.name = name;
        this.animal = animal;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Animal getAnimal() {
        return animal;
    }

    public void setAnimal(Animal animal) {
        this.animal = animal;
    }

    @Override
    public String toString() {
        return "AnimalOwner [id=" + id + ", name=" + name + ", animal=" + animal + "]";
    }

}

6、創建Worker實體類Worker.java

package com.spring.model;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class Worker {

    private int id;
    private String name;
    private List<Animal> animals = new LinkedList<Animal>();
    private List<String> phones = new LinkedList<String>();
    private Map<String, String> works = new HashMap<String, String>();
    private Properties family = new Properties();

    public Worker() {
        super();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public List<Animal> getAnimals() {
        return animals;
    }

    public void setAnimals(List<Animal> animals) {
        this.animals = animals;
    }

    public List<String> getPhones() {
        return phones;
    }

    public void setPhones(List<String> phones) {
        this.phones = phones;
    }

    public Map<String, String> getWorks() {
        return works;
    }

    public void setWorks(Map<String, String> works) {
        this.works = works;
    }

    public Properties getFamily() {
        return family;
    }

    public void setFamily(Properties family) {
        this.family = family;
    }

    @Override
    public String toString() {
        return "Worker [id=" + id + ", name=" + name + ", animals=" + animals +
                ", phones=" + phones + ", works=" + works + ", family=" + family + "]";
    }

}

7、創建spring配置文件applicationContext.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">

    <!--注入基本類型-->
    <bean id="dog" class="com.spring.model.Animal">
        <property name="species" value="Dog: Tom"></property>
    </bean>

    <!--注入bean-->
    <bean id="people1" class="com.spring.model.People">
        <property name="id" value="1"></property>
        <property name="name" value="趙一"></property>
        <property name="animal" ref="dog"></property>
    </bean>

    <!--內部bean-->
    <bean id="people2" class="com.spring.model.People">
        <property name="id" value="2"></property>
        <property name="name" value="錢二"></property>
        <property name="animal">
            <bean class="com.spring.model.Animal">
                <property name="species" value="tiger"></property>
            </bean>
        </property>
    </bean>

    <!--注入null-->
    <bean id="people3" class="com.spring.model.People">
        <property name="id" value="3"></property>
        <property name="name" value="孫三"></property>
        <property name="animal">
            <null></null>
        </property>
    </bean>

    <!--級聯屬性-->
    <bean id="animalOwner" class="com.spring.model.AnimalOwner">
        <property name="id" value="0"></property>
        <property name="name" value="動物的主人"></property>
        <property name="animal.species" value="Cat: Kate"></property>
    </bean>

    <!--注入集合-->
    <bean id="worker" class="com.spring.model.Worker">
        <property name="id" value="9"></property>
        <property name="name" value="程序員"></property>
        <property name="animals">
            <list>
                <bean class="com.spring.model.Animal">
                    <property name="species" value="dog"></property>
                </bean>
                <bean class="com.spring.model.Animal">
                    <property name="species" value="cat"></property>
                </bean>
                <bean class="com.spring.model.Animal">
                    <property name="species" value="tiger"></property>
                </bean>
            </list>
        </property>
        <property name="phones">
            <list>
                <value>15880111111</value>
                <value>15880222222</value>
                <value>15880333333</value>
            </list>
        </property>
        <property name="works">
            <map>
                <entry>
                    <key><value>上午</value></key>
                    <value>寫程序</value>
                </entry>
                <entry>
                    <key><value>下午</value></key>
                    <value>調程序</value>
                </entry>
                <entry>
                    <key><value>晚上</value></key>
                    <value>寫文檔</value>
                </entry>
            </map>
        </property>
        <property name="family">
            <props>
                <prop key="父親">梁山伯</prop>
                <prop key="母親">祝英臺</prop>
            </props>
        </property>
    </bean>

</beans>

8、創建Spring測試類SpringUnit.java

package com.spring.junit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.model.Animal;
import com.spring.model.AnimalOwner;
import com.spring.model.People;
import com.spring.model.Worker;

public class SpringUnit {

    ClassPathXmlApplicationContext ctx = null;

    @Before
    public void setUp() throws Exception {
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void test() {

        Animal animal = (Animal) ctx.getBean("dog");
        System.out.println(animal.toString());

        People people = null;

        people = (People) ctx.getBean("people1");
        System.out.println(people.toString());

        people = (People) ctx.getBean("people2");
        System.out.println(people.toString());

        people = (People) ctx.getBean("people3");
        System.out.println(people.toString());

        AnimalOwner animalOwner = (AnimalOwner) ctx.getBean("animalOwner");
        System.out.println(animalOwner.toString());

        Worker worker = (Worker) ctx.getBean("worker");
        System.out.println(worker.toString());

    }

    @After
    public void tearDown() throws Exception {
        ctx.close();
    }

}

9、測試結果

... 省略Spring日誌信息 ...

Animal [species=Dog: Tom]
People [id=1, name=趙一, animal=Animal [species=Dog: Tom]]
People [id=2, name=錢二, animal=Animal [species=tiger]]
People [id=3, name=孫三, animal=null]
AnimalOwner [id=0, name=動物的主人, animal=Animal [species=Cat: Kate]]
Worker [id=9, name=程序員, animals=[Animal [species=dog], Animal [species=cat], Animal [species=tiger]], phones=[15880111111, 15880222222, 15880333333], works={上午=寫程序, 下午=調程序, 晚上=寫文檔}, family={母親=祝英臺, 父親=梁山伯}]

... 省略Spring日誌信息 ...
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章