原文:BeanFactory 和 ApplicationContext
Spring通过一个配置文件描述Bean及Bean直接的依赖关系,利用Java语言的反射功能实例化Bean并建立Bean之间的依赖关系。Sprig的IoC容器在完成这些底层工作的基础上,还提供了Bean实例缓存、生命周期管理、Bean实例代理、事件发布、资源装载等高级服务。
Bean工厂(com.springframework.beans.factory.BeanFactory)是Spring框架最核心的接口,它提供了高级IoC的配置机制。BeanFactory使管理不同类型的Java对象成为可能,应用上下文(com.springframework.context.ApplicationContext)建立在BeanFactory基础之上,提供了更多面向应用的功能,它提供了国际化支持和框架事件体系,更易于创建实际应用。我们一般称BeanFactory为IoC容器,而称 ApplicationContext为应用上下文。但有时为了行文方便,我们也将ApplicationContext称为Spring容器。
BeanFactory是Spring框架的基础设施,面向Spring本身;ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合都直接使用ApplicationContext而非底层的BeanFactory。
1、初始化BeanFactory
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.0.xsd"> <bean id="car" class="com.reflect.Car" p:brand="迈锐宝" p:color="黑色" p:maxSpeed="300"/> </beans>
BeanFactoryTest:
package com.beanfactory; import com.reflect.Car; 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; import java.io.IOException; /** * Created by gao on 16-3-18. */ public class BeanFactoryTest { public static void main(String[] args) throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource res = resolver.getResource("classpath:beans.xml"); BeanFactory bf = new XmlBeanFactory(res); System.out.println("init BeanFactory."); Car car = bf.getBean("car", Car.class); System.out.println("car bean is ready for use!"); } }
在初始化BeanFactory时,必须为其提供一种日志框架,我们使用Log4J,即在类路径下提供Log4J配置文件,这样启动Spring容器才不会报错。
log4j.properties
log4j.rootLogger=INFO,A1 log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n
测试输出:
2016-03-18 17:19:27,045 INFO [main] (XmlBeanDefinitionReader.java:315) - Loading XML bean definitions from class path resource [beans.xml]
init BeanFactory.
car bean is ready for use!
2、ApplicationContext
ApplicationContext使用ClassPathXmlApplicationContext和FileSystemXMLApplicationContext,前者默认从类路径下加载配置文件,后者默认从文件系统中装载配置文件。在获取ApplicationContext实例后,就可以像BeanFactory一样调用getBean(beanName)返回Bean了。BeanFactory在初始化容器时,并未实例化Bean,直到第一次访问某个Bean时才实例目标Bean;而ApplicationContext则在初始化应用上下文时就实例化所以单实例的Bean。
Beans:
package com.context; import com.reflect.Car; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.context.annotation.Bean; /** * Created by gao on 16-3-18. */ @Configurable public class Beans { @Bean(name = "car") public Car buildCar() { Car car = new Car(); car.setBrand("英菲迪尼"); car.setMaxSpeed(300); return car; } }
AnnotationApplicationContext:
package com.context; import com.reflect.Car; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * Created by gao on 16-3-18. */ public class AnnotationApplicationContext { public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(Beans.class); Car car = ctx.getBean("car", Car.class); System.out.println(car.getBrand()); System.out.println(car.getMaxSpeed()); } }