spring boot factory bean
Spring中有兩種bean, 用的最多的是 annotationBean,但是對於一些客製化的需求,可以使用 factoryBean, 它提供了三個介面
@Nullable
T getObject() throws Exception;
@Nullable
Class getObjectType();
default boolean isSingleton() {
return true;
}
定義
@Component
class AutoMoneyHouseExtApiProxyCreator : BeanFactoryPostProcessor {
private val logger: Logger = LogManager.getLogger()
override fun postProcessBeanFactory(beanFactory: ConfigurableListableBeanFactory) {
ServiceProvider::class.java.declaredFields.filter {
it.isEnumConstant && it.isAnnotationPresent(Rail::class.java)
}.forEach {
val railId = it.getAnnotation(Rail::class.java).id
val provider = it.get(null) as ServiceProvider
createApiProxy(railId, provider, beanFactory as BeanDefinitionRegistry)
}
}
private fun createApiProxy(railId: String, provider: ServiceProvider,
registry: BeanDefinitionRegistry) {
logger.info("auto-generate api proxy for: {}", provider)
val mhId = provider.providerName
// Payment API
if (provider.services.contains(Service.PAYMENT)) {
val railsDisbBeanDef = BeanDefinitionBuilder.rootBeanDefinition(DisbursementRpcServiceProxyFactoryBean::class.java)
.addConstructorArgValue(railId)
.addConstructorArgValue(mhId)
.beanDefinition
val railsDisbBeanName = "rails-disb-${provider.name}"
registry.registerBeanDefinition(railsDisbBeanName, railsDisbBeanDef)
val paymentBeanDef = BeanDefinitionBuilder.rootBeanDefinition(DisbursementCPAPIImpl::class.java)
.addConstructorArgValue(mhId)
.addConstructorArgReference(railsDisbBeanName)
.addConstructorArgValue(provider)
.beanDefinition
registry.registerBeanDefinition(Service.PAYMENT.name + "-" + provider.name, paymentBeanDef)
}
// Balance API
val railsBalBeanDef = BeanDefinitionBuilder.rootBeanDefinition(AccountBalanceRpcServiceProxyFactoryBean::class.java)
.addConstructorArgValue(railId)
.addConstructorArgValue(mhId)
.beanDefinition
val railsBalBeanName = "rails-bal-${provider.name}"
registry.registerBeanDefinition(railsBalBeanName, railsBalBeanDef)
val balanceBeanDef = BeanDefinitionBuilder.rootBeanDefinition(BalanceAPIServiceAdapter::class.java)
.addConstructorArgValue(mhId)
.addConstructorArgReference(railsBalBeanName)
.beanDefinition
registry.registerBeanDefinition("BALANCE-" + provider.name, balanceBeanDef)
}
}
class DisbursementRpcServiceProxyFactoryBean(private val railId: String, private val mhId: MoneyHouseID)
: FactoryBean {
@Autowired
lateinit var restTemplate: RestTemplate
@Value("\${application.railsGateway.url}")
var railsGatewayUrl: String = ""
override fun getObject(): DisbursementRpcService {
val client = JsonRpcRestClient(URL(URL(railsGatewayUrl), railId + DisbursementRpcService.SERVICE_PATH),
Codecs.NO_NULL_FIELD_ORDERED_MAPPER, restTemplate,
mapOf("mhId" to mhId))
return ProxyUtil.createClientProxy(javaClass.classLoader, DisbursementRpcService::class.java, client)
}
override fun getObjectType() = DisbursementRpcService::class.java
}
class AccountBalanceRpcServiceProxyFactoryBean(private val railId: String, private val mhId: MoneyHouseID)
: FactoryBean {
@Autowired 無錫人流醫院哪家好
lateinit var restTemplate: RestTemplate
@Value("\${application.railsGateway.url}")
var railsGatewayUrl: String = ""
override fun getObject(): AccountBalanceRpcService {
val client = JsonRpcRestClient(URL(URL(railsGatewayUrl), railId + AccountBalanceRpcService.SERVICE_PATH),
Codecs.NO_NULL_FIELD_ORDERED_MAPPER, restTemplate,
mapOf("mhId" to mhId))
return ProxyUtil.createClientProxy(javaClass.classLoader, AccountBalanceRpcService::class.java, client)
}
override fun getObjectType() = AccountBalanceRpcService::class.java
}
使用
@Target(AnnotationTarget.FIELD)
annotation class Rail(val id: String)
enum class ServiceProvider constructor(val providerName: String, vararg list: Service) {
@Rail("ttt") TTTT(MHHIDs.TTTT, Service.CONVERSION, Service.PAYMENT)
val services: Array = arrayOf(*list)
}
精髓
生成beanDef:
BeanDefinitionBuilder.rootBeanDefinition(DisbursementRpcServiceProxyFactoryBean::class.java)
.addConstructorArgValue(railId)
.addConstructorArgValue(mhId)
.beanDefinition
註冊bean
registry.registerBeanDefinition(railsDisbBeanName, railsDisbBeanDef)
其中 registry:
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
registry = beanFactory
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69945560/viewspace-2655713/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 在Spring Boot快取API - Code FactorySpring Boot快取API
- Spring Boot中從自定義Logback訪問Spring Bean三種方法Spring BootBean
- Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of typeSpringFrameworkBeanException
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'aaaa' availableSpringFrameworkBeanExceptionAI
- Spring Boot 啟動原始碼解析結合Spring Bean生命週期分析Spring Boot原始碼Bean
- 在Spring boot中通過ApplicationContext獲取bean失敗Spring BootAPPContextBean
- [Spring]BeanSpringBean
- 報錯:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xxx'is definedSpringFrameworkBeanException
- Spring Bean容器SpringBean
- 【Spring】Bean管理SpringBean
- Spring Boot之輸入Bean驗證@Valid應用場景總結Spring BootBean
- Spring Boot:Spring Boot配置MybatisSpring BootMyBatis
- Spring Boot:Spring Boot配置SwaggerSpring BootSwagger
- Spring bean 裝配SpringBean
- Spring Bean 綜述SpringBean
- Spring Bean作用域SpringBean
- 淺談Spring BeanSpringBean
- Spring基礎(Bean)SpringBean
- Spring Bean 詳解SpringBean
- Spring裝配Bean(六)Bean的作用域SpringBean
- spring、spring MVC、spring BootMVCSpring Boot
- Spring Boot系列十九 Spring boot整合 swaggerSpring BootSwagger
- Spring Boot 參考指南(Spring Boot文件)Spring Boot
- Spring Boot 2.0(八):Spring Boot 整合 MemcachedSpring Boot
- Spring裝配Bean(四)SpringBean
- Spring裝配Bean(一)SpringBean
- Spring裝配Bean(二)SpringBean
- 二、Spring裝配BeanSpringBean
- Spring系列.Bean簡介SpringBean
- 《Spring》(六)---- Bean的scopeSpringBean
- Spring Bean 生命週期SpringBean
- Spring Bean 標籤解析SpringBean
- Spring Bean生命週期SpringBean
- Spring Boot —— Spring SecuritySpring Boot
- Spring BootSpring Boot
- Spring IOC 一——Spring容器裝配BeanSpringBean
- Spring 學習筆記(2) Spring BeanSpring筆記Bean
- Jeecg-Boot Spring BootSpring Boot