深入学习Spring框架之二构造器注入方式装配Bean

    这一节我们来看看怎么使用Spring创建我们的Bean对象。

    容器是Spring的核心,Spring的容器有两种类型:Bean工厂,由BeanFactory接口定义,是最简单的容器;以及应用上下文,由ApplicationContext定义。Bean工厂对于大多数应用来说太低级了,因此应用上下文更为受欢迎。

    应用上下文又可以分为以下几种类型:

        ClassPathXmlApplicationContext----从类路径下的XML配置文件加载上下文,把应用上下文定义文件当作类资源

FileSystemXmlApplicationContext----读取文件系统下的XML配置文件并加载上下文定义

XmlWebApplicationContext----读取Web应用下的XML配置文件并装载上下文定义


    下面我们来应用Spring创建Bean对象,首先我们来模拟一个环境,G20峰会的晚会表演,据说很不赖,画面直接可以做壁纸

/**
 * 面向接口编程,表演者接口
 * @author liuyc
 *
 */
public interface Performer {
	public void perform();
}
/**
 * 歌手类
 * @author liuyc
 *
 */
@SuppressWarnings("all")
public class Singer implements Performer{
	
	private String song;
	
	private Instrument instrument;
	
	public Singer() {
	}

	public Singer(String song) {
		this.song = song;
	}
	
	public Singer(String song, Instrument instrument) {
		super();
		this.song = song;
		this.instrument = instrument;
	}

	@Override
	public void perform() {
		this.instrument.play(song);
		System.out.println("while singing ...");
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	
	<bean id="singer"  class ="com.cm2easy.miki.example.chapter1.Singer"></bean>

</beans>
这样我们就声明了一个简单Bean,在xml配置文件中我们没有进行任何多余的配置,这个时候spring默认是调用类的默认构造器,所产生的结果与我们的传统创建对象的方法是一样的:

Singer singer = new Singer();


我们还可以调用有参的构造器声明一个Bean:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	
	<bean id="singer"  class ="com.cm2easy.miki.example.chapter1.Singer">
		<constructor-arg value="听妈妈的话"></constructor-arg>
	</bean>

</beans>
这种构造器注入的方式相当于我们调用有参构造new一个对象,效果是一模一样的:

Singer singer = new Singer("听妈妈的话");


我们不仅可以像上面那样通过构造器注入基本类型的变量,还可以通过构造器将对象的引用注入:

/**
 * 乐器接口
 * @author liuyc
 *
 */
public interface Instrument {
	public void play(String melodyName);
}
/**
 * 钢琴类
 * @author liuyc
 *
 */
public class Piano implements Instrument{
	@Override
	public void play(String melodyName) {
		System.out.println("正在演奏" + melodyName);
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	
	<bean id="singer"  class ="com.cm2easy.miki.example.chapter1.Singer">
		<constructor-arg value="听妈妈的话"></constructor-arg>
		<constructor-arg ref="piano"></constructor-arg>
	</bean>
	<bean id="piano" class="com.cm2easy.miki.example.chapter1.Piano"></bean>
</beans>
如上所示,在调用public Singer(String song, Instrument instrument)构造器之前,先要声明Instrument的对象,再通过构造器注入生命singer对象,这样歌手不仅可以唱歌还可以演奏对应歌曲的钢琴曲了


构造器注入的方式声明Bean对象,无外乎上面的情况,掌握了以上配置要想通过构造器声明对象就可以游刃有余了。





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

L小芸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值