从头认识Spring-1.4 怎样通过构造器注入Bean?
来源:互联网 发布:云计算大数据什么意思 编辑:程序博客网 时间:2024/06/03 06:43
这一章节我们来介绍一下通过构造器注入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() {}@Overridepublic 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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsdhttp://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;@Overridepublic 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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsdhttp://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 {@Autowiredprivate ApplicationContext applicationContext;@Testpublic 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();}@Overridepublic 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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsdhttp://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。
目录:http://blog.csdn.net/raylee2007/article/details/50611627
我的github:https://github.com/raylee2015/my_new_spring
1 0
- 从头认识Spring-1.4 怎样通过构造器注入Bean?
- 从头认识Spring-1.7 怎样通过属性注入Bean?(1)-怎样通过属性向对象注入值?
- 从头认识Spring-1.7 怎样通过属性注入Bean?(2)-怎样通过属性向对象注入另一个对象的引用?
- 从头认识Spring-2.3 注解装配-@autowired(3)-通过构造器方法注入
- 从头认识Spring-2.4 基于java的标准注解装配-@Inject(3)-通过构造器方法注入
- 从头认识Spring-1.9 内部类注入Bean
- Spring的《XML显式装配bean》之通过构造器注入Bean
- Spring bean属性注入(构造器)
- spring bean的构造器注入
- 从头认识Spring-1.12 注入Map
- spring通过构造注入
- 从头认识Spring-2.3 注解装配-@autowired(1)-通过属性域注入
- 从头认识Spring-2.3 注解装配-@autowired(2)-通过set方法或者其他方法注入
- spring通过@Configuration @Bean注入Bean
- 从头认识Spring-1.3 Spring的容器与Bean
- 从头认识java-7.5 怎样通过继承扩展接口?
- 从头认识Spring-1.5 Bean的作用域
- 从头认识Spring-1.6 Bean的创建与销毁操作
- Mybatis一对多查询
- docker容器秒死的解决办法
- ios全景图片浏览App
- c#之枚举
- 使用终端安装 TeXLive
- 从头认识Spring-1.4 怎样通过构造器注入Bean?
- 类与对象的关系 局部变量 成员变量
- NetCDF中scale_factor和add_offset的使用
- CentOS 安装 nignx
- 看图识“字”(1)-看数据条知大小
- android屏幕适配之dimens适配
- NSString 中包含中文字符时转换为NSURL
- 修改记事本默认编码为UTF-8
- 快速掌握一个语言最常用的50%