Spring詳細(xì)講解FactoryBean接口的使用
FactoryBean是一個接口,創(chuàng)建對象的過程使用了工廠模式。
一、基本使用
讓Spring容器通過FactoryBean來實現(xiàn)對象的創(chuàng)建。
?? 創(chuàng)建FactoryBean案例
public class SimpleFactoryBean implements FactoryBean<Cat> {
@Override
public Cat getObject() throws Exception {
// 創(chuàng)建Cat對象并且返回
return new Cat("Tom");
}
@Override
public Class<?> getObjectType() {
return Cat.class;
}
}
其實這里就相當(dāng)于在配置文件里創(chuàng)建了一個bean標(biāo)簽。
<bean class="com.text.registrar.SimpleFactoryBean" id="cat"></bean>
?? 需要加載的類
public class Cat {
String name;
public Cat(String name) {
this.name = name;
}
}
?? 啟動類里面創(chuàng)建方法加載Bean
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(SpringbootApplication.class, args);
}
// 等價于配置文件配置
@Bean
public SimpleFactoryBean catFactoryBean() {
return new SimpleFactoryBean();
}
}
二、高級使用
讓Spring容器通過FactoryBean創(chuàng)建指定接口的動態(tài)代理對象。
創(chuàng)建FactoryBean,可以生成任意接口的動態(tài)代理對象
?? 創(chuàng)建一個接口
public interface MyUserMapper {
void select();
}
?? 創(chuàng)建一個FactoryBean實現(xiàn)類
可以通過傳入接口的全類名,創(chuàng)建動態(tài)代理對象。
public class MapperFactoryBean implements FactoryBean {
/**
* 用于保存接口的全類名
*/
private String className;
public MapperFactoryBean(String className) {
this.className = className;
}
@Override
public Object getObject() throws Exception {
Class<?> interfaceClass = Class.forName(className);
// 生成動態(tài)代理對象返回
Object proxyInstance = Proxy.newProxyInstance(MapperFactoryBean.class.getClassLoader(), new Class[]{interfaceClass}, new InvocationHandler() {
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
// 獲取當(dāng)前方法名
System.out.println(method.getName());
if ("select".equals((method.getName()))) {
System.out.println(className + "動態(tài)代理對象的select方法被執(zhí)行了??!");
}
return null;
}
});
return proxyInstance;
}
@Override
public Class<?> getObjectType() {
try {
Class<?> interfaceCLass = Class.forName(className);
return interfaceCLass;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}?? 啟動類里面創(chuàng)建方法加載Bean
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(SpringbootApplication.class, args);
run.getBean(MyUserMapper.class);
}
// 等價于配置文件配置
@Bean
public MapperFactoryBean mapperFactoryBean() {
return new MapperFactoryBean("com.test.registrar.MyUserMapper");
}
}
到此這篇關(guān)于Spring詳細(xì)講解FactoryBean接口的使用的文章就介紹到這了,更多相關(guān)Spring FactoryBean內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解SpringBoot Mybatis如何對接多數(shù)據(jù)源
這篇文章主要為大家介紹了SpringBoot Mybatis如何對接多數(shù)據(jù)源實現(xiàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
Mybatis動態(tài)SQL foreach標(biāo)簽用法實例
這篇文章主要介紹了Mybatis動態(tài)SQL foreach標(biāo)簽用法實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10
詳解關(guān)于SpringBoot的外部化配置使用記錄
這篇文章主要介紹了詳解關(guān)于SpringBoot的外部化配置使用記錄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
java線程之用Thread類創(chuàng)建線程的方法
本篇文章介紹了,Thread類創(chuàng)建線程的方法。需要的朋友參考下2013-05-05

