Spring BeanFactory和FactoryBean區(qū)別解析
BeanFactory接口:
IoC容器的頂級(jí)接口,是IoC容器的最基礎(chǔ)實(shí)現(xiàn),也是訪問(wèn)Spring容器的根接口,負(fù)責(zé)對(duì)bean的創(chuàng)建,訪問(wèn)等工作。
其實(shí)在容器的初始化的時(shí)候,會(huì)對(duì)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,通過(guò)實(shí)現(xiàn)該接口可以對(duì)bean進(jìn)行一些額外的操作,例如根據(jù)不同的配置類(lèi)型返回不同類(lèi)型的bean,簡(jiǎn)化xml配置等。在使用上也有些特殊,BeanFactory接口中有一個(gè)字符常量String FACTORY_BEAN_PREFIX = '&'; 當(dāng)我們?nèi)カ@取BeanFactory類(lèi)型的bean時(shí),如果beanName不加&則獲取到對(duì)應(yīng)bean的實(shí)例;
如果beanName加上&,則獲取到BeanFactory本身的實(shí)例;FactoryBean接口對(duì)應(yīng)Spring框架來(lái)說(shuō)占有重要的地位,Spring本身就提供了70多個(gè)FactoryBean的實(shí)現(xiàn)。他們隱藏了實(shí)例化一些復(fù)雜的細(xì)節(jié),給上層應(yīng)用帶來(lái)了便利。從Spring3.0開(kāi)始,F(xiàn)actoryBean開(kāi)始支持泛型。
代碼展示:
//創(chuàng)建一個(gè)Spring定義的FactoryBeanpublic class ColorFactoryBean implements FactoryBean<Color> { //返回一個(gè)Color對(duì)象,這個(gè)對(duì)象會(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>
測(cè)試類(lèi):
public class Test1 { ClassPathXmlApplicationContext xmlBeanFactory = null; @Before public void initXmlBeanFactory() { System.out.println('n========測(cè)試方法開(kāi)始=======n'); xmlBeanFactory = new ClassPathXmlApplicationContext('spring3.xml'); } @After public void after() { System.out.println('n========測(cè)試方法結(jié)束=======n'); } @Test public void test8() { System.out.println(xmlBeanFactory.getBean('colorFactoryBean')); System.out.println('==================='); System.out.println(xmlBeanFactory.getBean('&colorFactoryBean'));}}
測(cè)試結(jié)果:
========測(cè)試方法開(kāi)始=======十二月 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========測(cè)試方法結(jié)束=======
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. xml中的空格之完全解說(shuō)2. asp讀取xml文件和記數(shù)3. IE6/IE7/IE8/IE9中tbody的innerHTML不能賦值的完美解決方案4. 利用CSS制作3D動(dòng)畫(huà)5. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))6. 匹配模式 - XSL教程 - 47. WML語(yǔ)言的基本情況8. 小技巧處理div內(nèi)容溢出9. jsp cookie+session實(shí)現(xiàn)簡(jiǎn)易自動(dòng)登錄10. xpath簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
