这一章节我们继续上面的话题。
2.怎样通过属性向对象注入另一个对象的引用?
(1)domain
我们除了蛋糕类,还需要引用前面的厨师类
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7; public class Cake { private final int id = index++; private static int index = 0; private String name = ""; private double size = 0; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSize() { return size; } public void setSize(double size) { this.size = size; } public int getId() { return id; } @Override public String toString() { return "create the cake,its id:" + id + ", size:" + size + " inch ,name:" + name; } }
蛋糕类不变
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7; public class Chief { private Cake cake = null; public Cake getCake() { return cake; } private String name = ""; public Chief(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void setCake(Cake cake) { this.cake = cake; } private final int id = index++; public int getId() { return id; } private static int index = 0; public Cake makeOneCake() { return cake; } }
我们在厨师类上面加上蛋糕这一个属性,同时也为这个厨师加上名称属性
(2)测试类
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_7/ApplicationContext-test.xml" }) public class ChiefTest { @Autowired private ApplicationContext applicationContext; @Test public void testChief() { Chief chief = applicationContext.getBean(Chief.class); System.out.println("chief name:" + chief.getName()); System.out.println(chief.getName() + " make a cake ,cake‘s name :" + chief.makeOneCake().getName()); } }
主要负责get那个Bean,然后打印厨师的名称和所做的蛋糕
(3)配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="cake" class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7.Cake"> <property name="name" value="Blueberry Cheesecake" /> <property name="size" value="7" /> </bean> <bean id="chief" class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7.Chief"> <constructor-arg value="jack" /> <property name="cake" ref="cake" /> </bean> </beans>
配置文件里面
A.cake不变
B.在chief的Bean里面引用上面已经生产的cake的Bean,同时也在构造器里面赋予名称
但是这里需要注意一点的是:ref里面的值,必须是你配置文件里面拥有的Bean的id,这里严格大小写,也就是说
<bean id="chief" class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7.Chief"> <constructor-arg value="jack" /> <property name="cake" ref="Cake" /> </bean>
和
<bean id="chief" class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7.Chief"> <constructor-arg value="jack" /> <property name="cake" ref="cake" /> </bean>
两者完全是不一样的
测试输出:
chief name:jack
jack make a cake ,cake‘s name :Blueberry Cheesecake
总结:这一章节主要介绍了怎样通过属性向对象注入另一个对象的引用,还有里面需要注意的地方。
目录:http://blog.csdn.net/raylee2007/article/details/50611627
我的github:https://github.com/raylee2015/my_new_spring
时间: 2024-10-07 05:30:04