从头认识Spring-1.4 怎样通过构造器注入Bean?
2016-01-30 00:27
836 查看
这一章节我们来介绍一下通过构造器注入Bean。
1.怎样声明一个Bean?
(1)创建类
(2)配置XML
2.怎样通过构造器注入Bean?
下面以厨师***蛋糕为例子。
(1)创建厨师类
在构建厨师的对象时,我们把需要***的蛋糕的对象也传进去。
(2)创建蛋糕类
通过final来标识每一个对象的id
然后创建之后只是打印一句已经创建即可
(3)配置文件
配置文件这里主要通过Bean标签来配置
在Bean标签里面还可以写入constructor-arg,这里就是通过构造器注入,他里面有两个参数,一个是ref,引用上面某个bean,一个是value,直接写入值
(4)创建测试类
如果是单独的建立ApplicationContext,可以使用我上面注释的方式,由于我们这里是多项目放在一起,因此我引用的是绝对路径。
输出:
create the cake,its id:0
3.如果类没有公开的构造器,如果使用单例的类?
根据上面的代码,我们补充一个烤炉,而我们这个烤炉选择单例模式。
然后厨师那里加上烤炉这个工具:
配置的时候特别为烤炉配置一个工厂方法,factory-method就是为了单例这种情况来设置的:
测试输出:
use old ovencreate the cake,its id:0
总结:这一章节介绍了如何通过构造器注入Bean。
目录:/article/3641905.html
我的github:https://github.com/raylee2015/my_new_spring
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。
目录:/article/3641905.html
我的github:https://github.com/raylee2015/my_new_spring
相关文章推荐
- java-nio
- Java 正则表达式的用法及详细介绍
- Java Calendar类
- SpringMVC基础框架搭建
- 详解java解压缩zip和rar的工具类使用教程
- Eclipse实现文件在资源管理器打开并选中
- 使用maven创建的java项目如何添加resources文件夹
- 使用注解开发springmvc应用
- (JAVA)抓取其它服务器返回的数据
- springMVC入门项目demo
- [java04]java大数类
- JavaSE-注解
- JavaSE-枚举类
- JavaSE-泛型的使用练习
- JavaSE-泛型
- Java生成不重复的数的方法(精确到毫秒级别)
- 利用Spring中的HtmlUtils.htmlEscape(input)过滤html
- springmvc配合jsp/ajax/json,实现简单的jsp遍历后台数据
- JAVA移位运算符
- java web 快速开发工具-捷兔