为什么spring构造注入的优点推荐使用构造器注入

1588人阅读
Spring(54)
这一章节我们来介绍一下通过构造器注入Bean。1.怎样声明一个Bean?(1)创建类package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_2;
public class Song {
public Song(String name) {
this.name =
public Song() {
public String toString() {
return &the song:& +
(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& /&
2.怎样通过构造器注入Bean?下面以厨师制作蛋糕为例子。(1)创建厨师类package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4;
public class Chief {
private Cake cake =
public Chief(Cake cake) {//构造器注入的地方
this.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;
public String toString() {
return &create the cake,its 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标签来配置在Bean标签里面还可以写入constructor-arg,这里就是通过构造器注入,他里面有两个参数,一个是ref,引用上面某个bean,一个是value,直接写入值(4)创建测试类package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4;
import org.junit.T
import org.junit.runner.RunW
import org.springframework.beans.factory.annotation.A
import org.springframework.context.ApplicationC
import org.springframework.test.context.ContextC
import org.springframework.test.context.junit4.SpringJUnit4ClassR
@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 applicationC
public void testChief() {
Chief chief = applicationContext.getBean(Chief.class);
chief.makeOneCake();
如果是单独的建立ApplicationContext,可以使用我上面注释的方式,由于我们这里是多项目放在一起,因此我引用的是绝对路径。输出:create the cake,its id:03.如果类没有公开的构造器,如果使用单例的类?根据上面的代码,我们补充一个烤炉,而我们这个烤炉选择单例模式。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();
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 =
private Oven oven =
public Chief(Cake cake) {
this.cake =
public Chief(Cake cake, Oven oven) {
this.cake =
this.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 id=&oven&
class=&com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4.Oven&
factory-method=&getInstance& /&
测试输出:use old ovencreate the cake,its id:0&总结:这一章节介绍了如何通过构造器注入Bean。目录:&&我的github:&&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:643676次
积分:11554
积分:11554
排名:第1032名
原创:488篇
评论:248条
文章:50篇
阅读:120363
阅读:8594
文章:52篇
阅读:63744
文章:174篇
阅读:225883
阅读:1712
阅读:9130
文章:18篇
阅读:20585
阅读:8696
文章:31篇
阅读:27285
文章:152篇
阅读:166687
(7)(18)(28)(10)(46)(46)(49)(50)(49)(56)(92)(13)(3)(7)(14)(1)(1)(2)(2)(1)

我要回帖

更多关于 spring 构造函数注入 的文章

 

随机推荐