0

我已经花了几天时间寻找在我的属性类(注释为@ConfigurationProperties)中定义字符串属性的方法,该属性将是类的名称,实现指定的接口。就像 spring.datasource.driver-class-name 一样。

如果您的代码包含 java.sql.Driver 实现(或者您对 impls 有多个依赖项),那么当您编辑 application.properties 文件时,idea 会帮助您从实现列表中选择驱动程序

我想要同样的。

我把完成的项目放在这里:https ://github.com/ibelozor/test-spring-properties (kotlin, gradle)

我定义服务:

interface MyService {
    fun doSomething()
}

,实施[s]:

class ServiceAImpl: MyService {
    override fun doSomething()  {
        println("Do something implementation from ${this.javaClass.name}")
    }
}

, 特性:

/**
 * Service properties
 */
@Component
@ConfigurationProperties(prefix = "service")
data class MyServiceProps (
        /**
         * Device driver class name
         */
        var className: String = "ru.ibelozor.test.classname.DefaultServiceImpl"
) {
        fun getService(): MyService {
                val serviceClass = Class.forName(className)
                return MyService::class.java.cast(serviceClass.newInstance())
        }
}

,所以我可以将我的服务用作 bean(这很好用):

@Configuration
class TestConfig
@Autowired constructor(private val props: MyServiceProps) {
    @Bean
    fun getMyService(): MyService {
        return props.getService()
    }
}

但是当我编辑
spring.datasource.driver-class-name 的 application.properties 文件案例时,我希望这个想法建议我实现我的界面: 我的案例 - service.class-name=... 没有建议: spring.datasource.driver-class-name 的情况

我的案例 - service.class-name=... 没有建议

4

0 回答 0