網(wǎng)站首頁 編程語言 正文
目錄
一、BeanFactory
1、簡單介紹
2、定義方法
3、主要實現(xiàn)類(包括抽象類)
?4、使用方式
二、FactoryBean
1、簡單介紹
?2、定義方法
3、常用類
4、使用方式
三、主要區(qū)別
一、BeanFactory
1、簡單介紹
這個其實是所有Spring Bean的容器根接口,給Spring 的容器定義一套規(guī)范,給IOC容器提供了一套完整的規(guī)范,比如我們常用到的getBean方法等。
進(jìn)入到這個類,我們可以看到如下注釋,意思是:訪問Spring bean容器的根接口。
2、定義方法
- getBean(String name):?Spring容器中獲取對應(yīng)Bean對象的方法,如存在,則返回該對象。
- containsBean(String name):判斷Spring容器中是否存在該對象。
- isSingleton(String name):通過beanName判斷是否為單例對象。
- isPrototype(String name):判斷bean對象是否為多例對象。
- isTypeMatch(String name, ResolvableType typeToMatch):判斷name值獲取出來的bean與typeToMath是否匹配。
- getType(String name):獲取Bean的Class類型。
- getAliases(String name):獲取name所對應(yīng)的所有的別名。
3、主要實現(xiàn)類(包括抽象類)
- AbstractBeanFactory:抽象Bean工廠,絕大部分的實現(xiàn)類,都是繼承于它。
- DefaultListableBeanFactory:Spring默認(rèn)的工廠類。
- XmlBeanFactory:前期使用XML配置用的比較多的時候用的Bean工廠。
- AbstractXmlApplicationContext:抽象應(yīng)用容器上下文對象。
- ClassPathXmlApplicationContext:XML解析上下文對象,用戶創(chuàng)建Bean對象我們早期寫Spring的時候用的就是它。
?4、使用方式
BeanFactory的使用方式有很多,這里就不一一列舉了,具體請查看源碼。
舉一個簡單的例子,使用ClassPathXmlApplicationContext讀取對應(yīng)的xml文件實例對應(yīng)上下文對象:
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
BeanFactory factory = (BeanFactory) context;
二、FactoryBean
1、簡單介紹
該類是SpringIOC容器是創(chuàng)建Bean的一種形式,這種方式創(chuàng)建Bean會有加成方式,融合了簡單的工廠設(shè)計模式于裝飾器模式。
* Interface to be implemented by objects used within a {@link BeanFactory} which * are themselves factories for individual objects. If a bean implements this * interface, it is used as a factory for an object to expose, not directly as a * bean instance that will be exposed itself.
有些人就要問了,我直接使用Spring默認(rèn)方式創(chuàng)建Bean不香么,為啥還要用FactoryBean做啥,在某些情況下,對于實例Bean對象比較復(fù)雜的情況下,使用傳統(tǒng)方式創(chuàng)建bean會比較復(fù)雜,例如(使用xml配置),這樣就出現(xiàn)了FactoryBean接口,可以讓用戶通過實現(xiàn)該接口來自定義該Bean接口的實例化過程。即包裝一層,將復(fù)雜的初始化過程包裝,讓調(diào)用者無需關(guān)系具體實現(xiàn)細(xì)節(jié)。
?2、定義方法
- T getObject():返回實例
- Class<?> getObjectType():返回該裝飾對象的Bean的類型
- default boolean isSingleton():Bean是否為單例
3、常用類
- ProxyFactoryBean:Aop代理Bean
- GsonFactoryBean:Gson
4、使用方式
Spring XML方式:
application.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="demo" class="cn.lonecloud.spring.example.bo.Person">
<property name="age" value="10"/>
<property name="name" value="xiaoMing"/>
</bean>
<bean id="demoFromFactory" class="cn.lonecloud.spring.example.bean.PersonFactoryBean">
<property name="initStr" value="10,init from factory"/>
</bean>
</beans>
personFactoryBean
package cn.lonecloud.spring.example.bean;
import cn.lonecloud.spring.example.bo.Person;
import org.springframework.beans.factory.FactoryBean;
import java.util.Objects;
public class PersonFactoryBean implements FactoryBean<Person> {
/**
* 初始化Str
*/
private String initStr;
@Override
public Person getObject() throws Exception {
//這里我需要獲取對應(yīng)參數(shù)
Objects.requireNonNull(initStr);
String[] split = initStr.split(",");
Person p=new Person();
p.setAge(Integer.parseInt(split[0]));
p.setName(split[1]);
//這里可能需要還要有其他復(fù)雜事情需要處理
return p;
}
@Override
public Class<?> getObjectType() {
return Person.class;
}
public String getInitStr() {
return initStr;
}
public void setInitStr(String initStr) {
this.initStr = initStr;
}
}
main方法
package cn.lonecloud.spring.example.factory;
import cn.lonecloud.spring.example.bo.Person;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* BeanFactory 例子
*/
public class SpringBeanFactoryMain {
public static void main(String[] args) {
//這個是BeanFactory
BeanFactory beanFactory = new ClassPathXmlApplicationContext("application.xml");
//獲取對應(yīng)的對象化
Object demo = beanFactory.getBean("demo");
System.out.println(demo instanceof Person);
System.out.println(demo);
//獲取從工廠Bean中獲取對象
Person demoFromFactory = beanFactory.getBean("demoFromFactory", Person.class);
System.out.println(demoFromFactory);
//獲取對應(yīng)的personFactory
Object bean = beanFactory.getBean("demoFromFactory");
System.out.println(bean instanceof PersonFactoryBean);
PersonFactoryBean factoryBean=(PersonFactoryBean) bean;
System.out.println("初始化參數(shù)為:"+factoryBean.getInitStr());
}
}
輸出內(nèi)容:
true
Person{name='xiaoMing', age=10}
Person{name='init from factory', age=10}
true
初始化參數(shù)為:10,init from factory
三、主要區(qū)別
- BeanFactory:負(fù)責(zé)生產(chǎn)和管理Bean的一個工廠接口,提供一個Spring Ioc容器規(guī)范。
- FactoryBean:一種Bean創(chuàng)建的一種方式,對Bean的一種擴展。對于復(fù)雜的Bean對象初始化創(chuàng)建使用其可封裝對象的創(chuàng)建細(xì)節(jié)。
原文鏈接:https://evanwang.blog.csdn.net/article/details/122811855
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-07-21 Python中直接賦值、淺拷貝和深拷貝的區(qū)別
- 2023-02-27 pandas?pd.cut()與pd.qcut()的具體實現(xiàn)_python
- 2022-03-26 c語言實現(xiàn)可自定義的游戲地圖_C 語言
- 2022-02-14 flutter封裝自定義打印信息
- 2022-08-19 python查看自己安裝的所有庫并導(dǎo)出的命令_python
- 2023-02-12 JetpackCompose?Scaffold組件使用教程_Android
- 2022-06-18 Android實現(xiàn)左滑關(guān)閉窗口_Android
- 2024-03-13 QAobject修改excel字體亂碼問題
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支