这一节我们来看看怎么使用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();
<?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对象,无外乎上面的情况,掌握了以上配置要想通过构造器声明对象就可以游刃有余了。