Spring BeanFactory和FactoryBean區(qū)別解析
BeanFactory接口:
IoC容器的頂級接口,是IoC容器的最基礎實現(xiàn),也是訪問Spring容器的根接口,負責對bean的創(chuàng)建,訪問等工作。
其實在容器的初始化的時候,會對BeanFactory做很多事情,如:
obtainFreshBeanFactory();獲取BeanFactory;
prepareBeanFactory(beanFactory);BeanFactory的預準備工作(BeanFactory進行一些設置)
postProcessBeanFactory(beanFactory);BeanFactory準備工作完成后進行的后置處理工作;
invokeBeanFactoryPostProcessors(beanFactory);執(zhí)行BeanFactoryPostProcessor的方法;
BeanFactoryPostProcessor:BeanFactory的后置處理器。在BeanFactory標準初始化之后執(zhí)行的;
FactoryBean接口:
可以返回bean的實例的工廠bean,通過實現(xiàn)該接口可以對bean進行一些額外的操作,例如根據(jù)不同的配置類型返回不同類型的bean,簡化xml配置等。在使用上也有些特殊,BeanFactory接口中有一個字符常量String FACTORY_BEAN_PREFIX = '&'; 當我們?nèi)カ@取BeanFactory類型的bean時,如果beanName不加&則獲取到對應bean的實例;
如果beanName加上&,則獲取到BeanFactory本身的實例;FactoryBean接口對應Spring框架來說占有重要的地位,Spring本身就提供了70多個FactoryBean的實現(xiàn)。他們隱藏了實例化一些復雜的細節(jié),給上層應用帶來了便利。從Spring3.0開始,F(xiàn)actoryBean開始支持泛型。
代碼展示:
//創(chuàng)建一個Spring定義的FactoryBeanpublic class ColorFactoryBean implements FactoryBean<Color> { //返回一個Color對象,這個對象會添加到容器中 @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:這個bean是單實例,在容器中保存一份 //false:多實例,每次獲取都會創(chuàng)建一個新的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========測試方法結束=======n'); } @Test public void test8() { System.out.println(xmlBeanFactory.getBean('colorFactoryBean')); System.out.println('==================='); System.out.println(xmlBeanFactory.getBean('&colorFactoryBean'));}}
測試結果:
========測試方法開始=======十二月 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========測試方法結束=======
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. python GUI庫圖形界面開發(fā)之PyQt5信號與槽的高級使用技巧裝飾器信號與槽詳細使用方法與實例2. python b站視頻下載的五種版本3. JavaScript設計模式之策略模式實現(xiàn)原理詳解4. JAVA抽象類及接口使用方法解析5. IntelliJ IDEA安裝插件的方法步驟6. python如何寫個俄羅斯方塊7. Intellj Idea中的maven工程Java文件顏色不對,未被識別的解決8. 《CSS3實戰(zhàn)》筆記--漸變設計(一)9. 如何通過vscode運行調試javascript代碼10. vscode運行php報錯php?not?found解決辦法
