Spring IoC容器类型– ApplicationContext和BeanFactory
Spring框架的主要组件之一是Spring IOC容器,它负责实例化bean,连接依赖关系并管理从实例化到销毁的bean生命周期。 org.springframework.beans和org.springframework.context包是SpringFramework的IoC容器的基础。基于这两个软件包,Spring容器有两种类型
- BeanFactory
- ApplicationContext
pring BeanFactory
org.springframework.beans.factory.BeanFactory是访问Spring bean容器的根接口。此接口的实现提供了一个对象,该对象包含许多Bean定义,每个定义均由String名称唯一标识。
BeanFactory将加载存储在配置源中的Bean定义,该配置元数据以XML,Java注释或者Java代码表示。例如,XmlBeanFactory是BeanFactory的实现,该实现从XML文档读取Bean定义。
请注意,Spring 3.x不推荐使用XmlBeanFactory类,而应将DefaultListableBeanFactory与XmlBeanDefinitionReader结合使用。
Spring BeanFactory示例
在该示例中,有一个用于下达订单的类称为Order,可以从零售商店进行购买。在Order类中,必须注入商店的依赖项。 XML配置用于bean定义。让我们看看BeanFactory如何用于加载bean定义和访问那些注册的bean。
豆类
public interface OrderService {
public void buyItems();
}
public class OrderServiceImpl implements OrderService {
private IStore store;
public OrderServiceImpl(IStore store){
this.store = store;
}
public void buyItems() {
store.doPurchase();
}
}
public interface IStore {
public void doPurchase();
}
public class RetailStore implements IStore {
public void doPurchase() {
System.out.println("Doing purchase from Retail Store");
}
}
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Store bean -->
<bean id="store" class="com.theitroad.springproject.service.RetailStore" />
<!-- Order bean with dependencies -->
<bean id="orderBean" class="com.theitroad.springproject.service.OrderServiceImpl">
<constructor-arg ref="store" />
</bean>
</beans>
下列类用于创建BeanFactory,该BeanFactory读取配置以加载Bean定义,连接依赖项并访问这些Bean。
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import com.theitroad.springproject.service.OrderService;
import com.theitroad.springproject.service.OrderServiceImpl;
public class App {
public static void main( String[] args ){
// BeanFactory
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
// loading bean definitions
reader.loadBeanDefinitions(new ClassPathResource("appContext.xml"));
OrderService order = factory.getBean(OrderServiceImpl.class);
order.buyItems();
}
}
输出:
17:51:24.668 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 2 bean definitions from class path resource [appContext.xml] 17:51:24.701 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'orderBean' 17:51:24.736 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'store' Doing purchase from Retail Store
这里,ClassPathResource用于从类路径读取配置,还有一个FileSystemResource用于从文件系统读取配置。
Spring ApplicationContext
org.springframework.context.ApplicationContext是BeanFactory的子接口,它在BeanFactory之上添加了更多企业特定的功能。 ApplicationContext添加的一些重要功能是
- 与Spring的AOP功能轻松集成
- 消息资源处理(用于国际化)
- 向事件侦听器发布事件
- 应用层特定的上下文,例如Web应用程序中使用的WebApplicationContext。
Spring文档更喜欢使用ApplicationContext而不是BeanFactory,因为它提供了BeanFactory的所有服务以及上述功能。在资源密集型应用程序中,即使仅稍微减少内存使用量也可以使用BeanFactory代替。
Spring框架具有ApplicationContext接口的几种实现。在独立应用程序中,通常创建ClassPathXmlApplicationContext(以读取驻留在类路径中的XML)或者FileSystemXmlApplicationContext(提供XML文件的完整路径以从文件系统读取)的实例。
在Web应用程序中,XmlWebApplicationContext用于将Web应用程序配置为XML文档。
Spring ApplicationContext示例
对于上面使用的示例配置,可以如下所示实例化ApplicationContext
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.theitroad.springproject.service.OrderService;
import com.theitroad.springproject.service.OrderServiceImpl;
public class App {
public static void main( String[] args ){
// create context using configuration
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appcontext.xml");
OrderService order = context.getBean("orderBean", OrderServiceImpl.class);
order.buyItems();
// close the context
context.close();
}
}
如果要使用FileSystemXmlApplicationContext,则必须将文件位置作为相对路径或者绝对路径传递。
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("F:\theitroad\Spring WorkSpace\SpringProject\src\main\resources\appContext.xml");

