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()); } }