Spring BeanFactory和FactoryBean區(qū)別解析
BeanFactory接口:
IoC容器的頂級接口,是IoC容器的最基礎(chǔ)實(shí)現(xiàn),也是訪問Spring容器的根接口,負(fù)責(zé)對bean的創(chuàng)建,訪問等工作。
其實(shí)在容器的初始化的時(shí)候,會(huì)對BeanFactory做很多事情,如:
obtainFreshBeanFactory();獲取BeanFactory;
prepareBeanFactory(beanFactory);BeanFactory的預(yù)準(zhǔn)備工作(BeanFactory進(jìn)行一些設(shè)置)
postProcessBeanFactory(beanFactory);BeanFactory準(zhǔn)備工作完成后進(jìn)行的后置處理工作;
invokeBeanFactoryPostProcessors(beanFactory);執(zhí)行BeanFactoryPostProcessor的方法;
BeanFactoryPostProcessor:BeanFactory的后置處理器。在BeanFactory標(biāo)準(zhǔn)初始化之后執(zhí)行的;
FactoryBean接口:
可以返回bean的實(shí)例的工廠bean,通過實(shí)現(xiàn)該接口可以對bean進(jìn)行一些額外的操作,例如根據(jù)不同的配置類型返回不同類型的bean,簡化xml配置等。在使用上也有些特殊,BeanFactory接口中有一個(gè)字符常量String FACTORY_BEAN_PREFIX = '&'; 當(dāng)我們?nèi)カ@取BeanFactory類型的bean時(shí),如果beanName不加&則獲取到對應(yīng)bean的實(shí)例;
如果beanName加上&,則獲取到BeanFactory本身的實(shí)例;FactoryBean接口對應(yīng)Spring框架來說占有重要的地位,Spring本身就提供了70多個(gè)FactoryBean的實(shí)現(xiàn)。他們隱藏了實(shí)例化一些復(fù)雜的細(xì)節(jié),給上層應(yīng)用帶來了便利。從Spring3.0開始,F(xiàn)actoryBean開始支持泛型。
代碼展示:
//創(chuàng)建一個(gè)Spring定義的FactoryBeanpublic class ColorFactoryBean implements FactoryBean<Color> { //返回一個(gè)Color對象,這個(gè)對象會(huì)添加到容器中 @Override public Color getObject() throws Exception { // TODO Auto-generated method stub System.out.println('ColorFactoryBean...getObject...'); return new Color(); } @Override public Class<?> getObjectType() { // TODO Auto-generated method stub return Color.class; } //是單例? //true:這個(gè)bean是單實(shí)例,在容器中保存一份 //false:多實(shí)例,每次獲取都會(huì)創(chuàng)建一個(gè)新的bean; @Override public boolean isSingleton() { // TODO Auto-generated method stub return false; }}
public class Color { private Car car; public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return 'Color [car=' + car + ']'; } }
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-4.0.xsd'> <bean class='spring2.ColorFactoryBean'></bean></beans>
測試類:
public class Test1 { ClassPathXmlApplicationContext xmlBeanFactory = null; @Before public void initXmlBeanFactory() { System.out.println('n========測試方法開始=======n'); xmlBeanFactory = new ClassPathXmlApplicationContext('spring3.xml'); } @After public void after() { System.out.println('n========測試方法結(jié)束=======n'); } @Test public void test8() { System.out.println(xmlBeanFactory.getBean('colorFactoryBean')); System.out.println('==================='); System.out.println(xmlBeanFactory.getBean('&colorFactoryBean'));}}
測試結(jié)果:
========測試方法開始=======十二月 09, 2019 4:49:52 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2e5c649: startup date [Mon Dec 09 16:49:52 CST 2019]; root of context hierarchy十二月 09, 2019 4:49:52 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [spring3.xml]ColorFactoryBean...getObject...Color [car=null]===================spring2.ColorFactoryBean@6ddf90b0========測試方法結(jié)束=======
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python如何批量生成和調(diào)用變量2. ASP.Net Core對USB攝像頭進(jìn)行截圖3. ASP.NET MVC實(shí)現(xiàn)橫向展示購物車4. Python 中如何使用 virtualenv 管理虛擬環(huán)境5. Python獲取B站粉絲數(shù)的示例代碼6. Python基于requests實(shí)現(xiàn)模擬上傳文件7. python利用opencv實(shí)現(xiàn)顏色檢測8. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無效問題9. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)10. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效
