005. Spring 自动装配

来源:互联网 发布:软件测试核心期刊 编辑:程序博客网 时间:2024/06/02 00:07

1、创建Java项目:File -> New -> Java Project

2、引入必要jar包,项目结构如下
这里写图片描述

3、创建Animal实体类Animal.java

package com.spring.model;public class Animal {    private String name = null;    public Animal() {        super();    }    public Animal(String name) {        super();        this.name = name;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public String toString() {        return "Animal [name=" + name + "]";    }}

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(Animal animal) {        super();        this.animal = animal;        System.out.println("Invoke People(Animal animal)");    }    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、创建spring配置文件applicationContext.xml

5.1、default-autowire=”byName”

注入条件:注入对象的id要和被注入对象属性名相同

<?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"         default-autowire="byName">    <bean id="animal" class="com.spring.model.Animal">        <property name="name" value="dog"></property>    </bean>    <!-- ByName注入条件:注入对象的id要和被注入对象属性名相同 -->    <bean id="people" class="com.spring.model.People">        <property name="id" value="0"></property>        <property name="name" value="People ByName"></property>    </bean></beans>

5.1.2、创建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.People;public class SpringUnit {    ClassPathXmlApplicationContext ctx = null;    @Before    public void setUp() throws Exception {        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");    }    @Test    public void test() {        People people = (People) ctx.getBean("people");        System.out.println(people.toString());    }    @After    public void tearDown() throws Exception {        ctx.close();    }}

5.1.3、测试结果

... 省略Spring日志信息 ...People [id=0, name=People ByName, animal=Animal [name=dog]]... 省略Spring日志信息 ...

5.2、default-autowire=”byType”

注入条件:注入对象的类型要和被注入对象属性类型相同

<?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"         default-autowire="byType">   <bean id="cat" class="com.spring.model.Animal">        <property name="name" value="cat"></property>    </bean>    <!-- ByType注入条件: 注入对象的类型要和被注入对象属性类型相同 -->    <bean id="people" class="com.spring.model.People">        <property name="id" value="0"></property>        <property name="name" value="People ByType"></property>    </bean></beans>

5.2.2、创建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.People;public class SpringUnit {    ClassPathXmlApplicationContext ctx = null;    @Before    public void setUp() throws Exception {        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");    }    @Test    public void test() {        People people = (People) ctx.getBean("people");        System.out.println(people.toString());    }    @After    public void tearDown() throws Exception {        ctx.close();    }}

5.2.3、测试结果

... 省略Spring日志信息 ...People [id=0, name=People ByType, animal=Animal [name=cat]]... 省略Spring日志信息 ...

5.3、default-autowire=”constructor”

注入条件:注入对象的类型要和被注入对象属性类型相同, 调用构造方法

<?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"         default-autowire="constructor">    <bean id="pig" class="com.spring.model.Animal">        <property name="name" value="pig"></property>    </bean>    <!-- constructor注入条件:注入对象的类型要和被注入对象属性类型相同, 调用构造方法 -->    <bean id="people" class="com.spring.model.People">        <property name="id" value="0"></property>        <property name="name" value="People Constructor"></property>    </bean></beans>

5.3.2、创建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.People;public class SpringUnit {    ClassPathXmlApplicationContext ctx = null;    @Before    public void setUp() throws Exception {        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");    }    @Test    public void test() {        People people = (People) ctx.getBean("people");        System.out.println(people.toString());    }    @After    public void tearDown() throws Exception {        ctx.close();    }}

5.3.3、测试结果

... 省略Spring日志信息 ...Invoke People(Animal animal)People [id=0, name=People Constructor, animal=Animal [name=pig]]... 省略Spring日志信息 ...
原创粉丝点击