004. Spring 注入参数

来源:互联网 发布:php use laravel 编辑:程序博客网 时间:2024/04/28 12:11

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日志信息 ...
原创粉丝点击