网站建设资讯

NEWS

网站建设资讯

Spring中有哪些bean

这篇文章主要讲解了“Spring中有哪些bean”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring中有哪些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无锡人流医院哪家好 http://www.ytsg029.com/

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

感谢各位的阅读,以上就是“Spring中有哪些bean”的内容了,经过本文的学习后,相信大家对Spring中有哪些bean这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


当前名称:Spring中有哪些bean
分享路径:http://cdweb.net/article/ihijie.html