1

我一直在努力在测试期间启动应用程序上下文,因此我可以在运行时注入 bean 但似乎我无法在这里连接 ApplicationContext 是我正在尝试的

  package com.wayfair.samworkgroupsservice.adapter

import io.r2dbc.mssql.MssqlConnectionConfiguration
import io.r2dbc.mssql.MssqlConnectionFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.config.ConstructorArgumentValues
import org.springframework.beans.factory.support.BeanDefinitionRegistry
import org.springframework.beans.factory.support.GenericBeanDefinition
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Configuration
import org.springframework.data.r2dbc.core.DefaultReactiveDataAccessStrategy
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate
import org.springframework.data.r2dbc.dialect.SqlServerDialect
import org.springframework.r2dbc.core.DatabaseClient
import org.springframework.test.context.ContextConfiguration

@Configuration
@ContextConfiguration("classpath:/testContext.xml")
class TemplateFactory {
    @Autowired
    lateinit var applicationContext: ApplicationContext

    private val beanFactory: BeanDefinitionRegistry =
        applicationContext.autowireCapableBeanFactory as BeanDefinitionRegistry

    fun registerTemplateBean(host: String, port: Int) {
        val beanDefinition = GenericBeanDefinition()
        beanDefinition.beanClass = R2dbcEntityTemplate::class.java
        val args = ConstructorArgumentValues()
        args.addIndexedArgumentValue(
            0,
            DatabaseClient.builder()
                .connectionFactory(connectionFactory(host, port))
                .bindMarkers(SqlServerDialect.INSTANCE.bindMarkersFactory)
                .build()
        )
        args.addIndexedArgumentValue(1, DefaultReactiveDataAccessStrategy(SqlServerDialect.INSTANCE))

        beanDefinition.constructorArgumentValues = args
        beanFactory.registerBeanDefinition("R2dbcEntityTemplate", beanDefinition)
    }

//    fun entityTemplate(host: String = "localhost", port: Int = 1435) =
//        R2dbcEntityTemplate(
//            DatabaseClient.builder()
//                .connectionFactory(connectionFactory(host, port))
//                .bindMarkers(SqlServerDialect.INSTANCE.bindMarkersFactory)
//                .build(),
//            DefaultReactiveDataAccessStrategy(SqlServerDialect.INSTANCE)
//        )

    private fun connectionFactory(host: String, port: Int) =
        MssqlConnectionFactory(
            MssqlConnectionConfiguration.builder()
                .host(host)
                .port(port)
                .username("sa")
                .password("Password123@#?")
                .build()
        )
}

这是我的testContext.xml(它现在是空的,因为我想在运行时注入 bean):

<?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-2.0.xsd">
    
</beans>

有谁知道为什么我会收到以下错误?

lateinit property applicationContext has not been initialized
kotlin.UninitializedPropertyAccessException: lateinit property applicationContext has not been initialized

先感谢您 )

4

0 回答 0