Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the bean in Spring

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what bean is in Spring". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what bean is in Spring.

There are two kinds of bean in Spring, the most commonly used is annotationBean, but for some customized requirements, you can use factoryBean, which provides three interfaces

@ Nullable

T getObject () throws Exception

@ Nullable

Class getObjectType ()

Default boolean isSingleton () {

Return true

}

Define

@ Component

Class AutoMoneyHouseExtApiProxyCreator: BeanFactoryPostProcessor {

Privateval 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)

.addConstructor ArgValue (railId)

.addConstructor ArgValue (mhId)

.beanDefinition

Val railsDisbBeanName = "rails-disb-$ {provider.name}"

Registry.registerBeanDefinition (railsDisbBeanName, railsDisbBeanDef)

Val paymentBeanDef = BeanDefinitionBuilder.rootBeanDefinition (DisbursementCPAPIImpl::class.java)

.addConstructor ArgValue (mhId)

.addConstructorArgReference (railsDisbBeanName)

.addConstructor ArgValue (provider)

.beanDefinition

Registry.registerBeanDefinition (Service.PAYMENT.name + "-" + provider.name, paymentBeanDef)

}

/ / Balance API

Val railsBalBeanDef = BeanDefinitionBuilder.rootBeanDefinition (AccountBalanceRpcServiceProxyFactoryBean::class.java)

.addConstructor ArgValue (railId)

.addConstructor ArgValue (mhId)

.beanDefinition

Val railsBalBeanName = "rails-bal-$ {provider.name}"

Registry.registerBeanDefinition (railsBalBeanName, railsBalBeanDef)

Val balanceBeanDef = BeanDefinitionBuilder.rootBeanDefinition (BalanceAPIServiceAdapter::class.java)

.addConstructor ArgValue (mhId)

.addConstructorArgReference (railsBalBeanName)

.beanDefinition

Registry.registerBeanDefinition ("BALANCE-" + provider.name, balanceBeanDef)

}

}

Class DisbursementRpcServiceProxyFactoryBean (privateval railId: String, privateval 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 (privateval railId: String, privateval mhId: MoneyHouseID)

: FactoryBean {

@ Autowired which is a good http://www.ytsg029.com/ in Wuxi abortion Hospital

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

}

Use

@ 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)

}

Essence

Generate beanDef:

BeanDefinitionBuilder.rootBeanDefinition (DisbursementRpcServiceProxyFactoryBean::class.java)

.addConstructor ArgValue (railId)

.addConstructor ArgValue (mhId)

.beanDefinition

Register for bean

Registry.registerBeanDefinition (railsDisbBeanName, railsDisbBeanDef)

Where registry:

Void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory) throws BeansException

Registry = beanFactory

Thank you for your reading, the above is the content of "what is bean in Spring". After the study of this article, I believe you have a deeper understanding of what bean is in Spring, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report