从头认识Spring-1.4 通过构造器注入Bean
Posted 李灵晖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从头认识Spring-1.4 通过构造器注入Bean相关的知识,希望对你有一定的参考价值。
这一章节我们来介绍一下通过构造器注入Bean。
1.怎样生命一个Bean?
(1)创建类
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_2; public class Song { private String name; public Song(String name) { this.name = name; } public Song() { } @Override public String toString() { return "the song:" + name; } }
(2)配置XML
<?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="song" class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_2.Song"> <constructor-arg value="there will be" /> </bean> </beans>
2.怎样通过构造器注入Bean?
下面以厨师制作蛋糕为例子。
(1)创建厨师类
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4; public class Chief { private Cake cake = null; public Chief(Cake cake) {//构造器注入的地方 this.cake = cake; } public void makeOneCake() { System.out.println(cake.toString()); } }
在构建厨师的对象时,我们把需要制作的蛋糕的对象也传进去。
(2)创建蛋糕类
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4; public class Cake { private final int id = index++; private static int index = 0; @Override public String toString() { return "create the cake,its id:" + id; } }
通过final来标识每一个对象的id
然后创建之后只是打印一句已经创建即可
(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_4.Cake" /> <bean id="chief" class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4.Chief"> <constructor-arg ref="cake" /> </bean> </beans>
配置文件这里主要通过Bean标签来配置
在Bean标签里面还可以写入constructor-arg,这里就是通过构造器注入,他里面有两个参数,一个是ref,引用上面某个bean,一个是value,直接写入值
(4)创建测试类
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4; 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 = { // "classpath:ApplicationContext-test.xml" }) @ContextConfiguration(locations = { "/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_4/ApplicationContext-test.xml" }) public class ChiefTest { @Autowired private ApplicationContext applicationContext; @Test public void testChief() { Chief chief = applicationContext.getBean(Chief.class); chief.makeOneCake(); } }
如果是单独的建立ApplicationContext,可以使用我上面注释的方式,由于我们这里是多项目放在一起,因此我引用的是绝对路径。
输出:
create the cake,its id:0
3.如果类没有公开的构造器,如果使用单例的类?
根据上面的代码,我们补充一个烤炉,而我们这个烤炉选择单例模式。
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4; public class Oven { private Oven() { } public static Oven getInstance() { return new Oven(); } @Override public String toString() { return "use old oven"; } }
然后厨师那里加上烤炉这个工具:
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4; public class Chief { private Cake cake = null; private Oven oven = null; public Chief(Cake cake) { this.cake = cake; } public Chief(Cake cake, Oven oven) { this.cake = cake; this.oven = oven; } public void makeOneCake() { System.out.println(oven.toString() + cake.toString()); } }
配置的时候特别为烤炉配置一个工厂方法,factory-method就是为了单例这种情况来设置的:
<?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_4.Cake" /> <bean id="chief" class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4.Chief"> <constructor-arg ref="cake" /> <constructor-arg ref="oven" /> </bean> <bean id="oven" class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4.Oven" factory-method="getInstance" /> </beans>
测试输出:
use old ovencreate the cake,its id:0
总结:这一章节介绍了如何通过构造器注入Bean。
我的github:https://github.com/raylee2015/my_new_spring
以上是关于从头认识Spring-1.4 通过构造器注入Bean的主要内容,如果未能解决你的问题,请参考以下文章
从头认识Spring-2.4 基于java的标准注解装配-@Inject-通过构造器方法注入
从头认识Spring-2.5 @Autowire @Inject @Qualifier @Named的相同与不同
从头认识Spring-1.7 怎样通过属性注入Bean?-怎样通过属性向对象注入值?