码迷,mamicode.com
首页 > 编程语言 > 详细

从头认识Spring-1.4 通过构造器注入Bean

时间:2016-01-30 02:45:16      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

这一章节我们来介绍一下通过构造器注入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

标签:

原文地址:http://blog.csdn.net/raylee2007/article/details/50608426

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!