Bean工厂(org.springframework.beans.factory.BeanFactory)是Spring框架最核心的接口,它提供了高级IoC的配置机制。BeanFactory使管理不同类型的Java对象成为可能,应用上下文(org.springframework.context.ApplicationContext)建立在BeanFactory基础之上,提供了更多面向应用的功能,它提供了国际化支持和框架事件体系,更易于创建实际应用。我们一般成为BeanFactory为IoC容器,而称ApplicationContext为应用上下文。但有时为了方便,也将ApplicationContext成为Spring容器。
对于两者的用途,我们可以进行简单划分:BeanFactory是Spring框架的基础设施,面向Spring本身;ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合我们都直接ApplicationContext而非底层的BeanFactory。
一、BeanFactory
BeanFactory是一个类工厂,但和传统的类工厂不同,传统的类工厂仅负责构造一个或几个类的实例。而BeanFactory是类的通用工厂,它可以创建并管理各种类的对象。这些可被创建和管理的对象本身没有什么特别之处,仅是POJO(简单的Java类),Spring称这些被创建个管理的Java对象为Bean,我们知道JavaBean是要满足一定规范的,如必须提供一个默认不带参的构造函数、不依赖于某一特定的容器等,但Spring中所说的Bean比JavaBean更宽泛一些,所有可以被Spring容器实例化并管理的Java类都可以成为Bean。
Spring为BeanFactory提供了多种实现类,最常用的是XmlBeanFactory。BeanFactory接口最主要的方式就是getBean(String beanName),该方法从容器中返回特定名称的Bean
举个例子:
1.Bean
Car.java文件
public class Car {
private String brand;
private String color;
private int maxSpeed;
public Car(){
}
public Car(String brand, String color, int maxSpeed) {
super();
this.brand = brand;
this.color = color;
this.maxSpeed = maxSpeed;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
//重写toString()方法可以在打印对象的时候输出对象的信息
@Override
public String toString() {
return "brand:"+brand+" color:"+color+" maxSpeed:"+maxSpeed;
}
}
2.初始化BeanFactory
使用Spring配置文件为Car提供配置信息,然后通过BeanFactory装载配置文件,启动Spring IoC容器
Car的配置文件——>beans.xml文件:
<?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.1.xsd">
<bean id="car1" class="com.bean.Car"
p:brand="宝马"
p:color="黑色"
p:maxSpeed="200"/>
</beans>
3.通过XmlBeanFactory实现类启动Spring IoC容器
XmlBeanFactory通过Resource装载Spring配置信息并启动IoC容器,然后就可以通过getBean(String beanName)方法从IoC容器中获取Bean了。
BeanFactoryTest文件(使用Junit进行单元测试):
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
public class BeanFactoryTest {
@Test
public void testBeanFactory(){
ResourcePatternResolver resolver=new PathMatchingResourcePatternResolver();
//通过BeanFactory装载配置文件
Resource resource=resolver.getResource("classpath:com/bean/beanfactory/beans.xml");
//通过XmlBeanFactory实现类启动Spring IoC容器
BeanFactory bf=new XmlBeanFactory(resource);
System.out.println("init BeanFactory");
//通过BeanFactory的getBean(String beanName)方法从容器中返回特定名称的Bean
Car car=(Car)bf.getBean("car1");
System.out.println(car);
System.out.println("car bean is ready for use");
}
}
注意:在初始化BeanFactory时,必须为其提供一种日志框架,我们使用Log4J,即在类路径下提供Log4J配置文件,这样启动Spring容器才不会报错。
所以在src下新建一个文件:
log4j.properties文件:
# Configure logging for testing: optionally with log file
log4j.rootLogger=WARN, stdout
# log4j.rootLogger=WARN, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
测试结果:
init BeanFactory
brand:宝马 color:黑色 maxSpeed:200
car bean is ready for use