这篇blog主要是介绍两点:
1. 怎样通过属性向对象注入值?
2. 怎样通过属性向对象注入另一个对象的引用?
这里的给对象注入值不是通过构造器注入,而是通过setter方法直接给对象的属性注入值。
1.怎样通过属性向对象注入值?
(1)蛋糕类Cake:这一个领域类我们只需要一个Cake就够了,但是我们在里面会加上名称(name)和大小(size)这两个属性,然后我们通过spring来帮我们赋值注入。
package spring.ch1.topic7_1;
/**
* Created by louyuting on 17/1/20.
* 注入属性,记得属性必须要写setter方法 不然就会抛出异常,注入失败.
*/
public class Cake { private static int index = 0; private final int id = index++; 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; } }
(2)配置文件
<?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-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--scope默认是单例的--> <bean id="cake" class="spring.ch1.topic7_1.Cake" scope="singleton"> <property name="name" value="Blueberry Cheesecake"/> <property name="size" value="7"/> </bean> </beans>
配置文件比较重要,我们在Bean里面需要插入property这个标签,然后name这个属性需要跟我们的domain类的属性名字一样,value表示对应的值。
注意:这里首字母可以不区分大小写
也就是
<bean id="cake"
class="spring.ch1.topic7_1.Cake"> <property name="Name" value="Blueberry Cheesecake" /> <property name="Size" value="7" /> </bean>
和
<bean id="cake"
class="spring.ch1.topic7_1.Cake"> <property name="name" value="Blueberry Cheesecake" /> <property name="size" value="7" /> </bean>
是一样的。
但是像下面的完全的大写,就会抛异常
<bean id="cake"
class="spring.ch1.topic7_1.Cake"> <property name="NAME" value="Blueberry Cheesecake" /> <property name="SIZE" value="7" /> </bean>
(3)测试类:没什么特别,只需要get那个Bean出来,然后打印一下几个属性即可。
package spring.ch1.topic7_1;
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; /** * Created by louyuting on 17/1/20. * 通过bean标签下的 property标签注入属性属性 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/spring/ch1/topic7_1/ApplicationContext-test.xml"}) public class CakeTest { @Autowired private ApplicationContext applicationContext; @Test public void testCake(){ Cake cake = (Cake)applicationContext.getBean("cake"); System.out.println(cake.getId()); System.out.println(cake.getName()); System.out.println(cake.getSize()); } }
(4)输出:
//output:~
/**
0
Blueberry Cheesecake
7.0
*/
这一章节主要介绍了怎样通过属性向对象注入值,还有中间需要注意的大小写的问题
完整代码的github地址。
2.怎样通过属性向对象注入另一个对象的引用?
(1)domain
我们除了蛋糕类,还需要引用前面的厨师类。
蛋糕类不变:
package spring.ch1.topic7_2;
/**
* Created by louyuting on 17/1/20.
* 注入属性,记得属性必须要写setter方法 不然就会抛出异常,注入失败.
*/
public class Cake { private static int index = 0; private final int id = index++; 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 spring.ch1.topic7_2;
/**
* Created by louyuting on 17/1/20.
*/
public class Chief { private Cake cake = null; private String name = ""; private final int id = index++; private static int index = 0; public Chief(String name) { this.name = name; } public Cake getCake() { return cake; } public void setCake(Cake cake) { this.cake = cake; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public Cake makeOneCake() { return cake; } }
(2)配置文件
<?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-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="cake" class="spring.ch1.topic7_2.Cake" scope="singleton"> <property name="name" value="Blueberry Cheesecake"/> <property name="size" value="7"/> </bean> <bean id="chief" class="spring.ch1.topic7_2.Chief"> <constructor-arg value="jack"></constructor-arg> <property name="cake" ref="cake"></property> </bean> </beans>
配置文件里面:
1).cake不变
2).在chief的Bean里面引用上面已经生产的cake的Bean;同时也在构造器里面赋予名称。
但是这里需要注意一点的是:ref里面的值,必须是你配置文件里面拥有的Bean的id,这里严格大小写,也就是说
<bean id="chief"
class="spring.ch1.topic7_2.Chief"> <constructor-arg value="jack" /> <property name="cake" ref="Cake" /> </bean>
和
<bean id="chief"
class="spring.ch1.topic7_2.Chief"> <constructor-arg value="jack" /> <property name="cake" ref="cake" /> </bean>
两者完全是不一样的。
(3)测试类:
package spring.ch1.topic7_2;
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; /** * Created by louyuting on 17/1/20. * 怎样通过属性向对象注入另一个对象的引用 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/spring/ch1/topic7_2/ApplicationContext-test.xml"}) public class CakeTest { @Autowired private ApplicationContext applicationContext; @Test public void testSinger(){ 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,然后打印厨师的名称和所做的蛋糕
(4)输出:
/**
output:~
chief name:jack
jack make a cake ,cake's name :Blueberry Cheesecake
*/
完整代码的github地址。