如何在 SpEL 中按类型引用 bean?

Tob*_*ann 7 spring spring-annotations spring-el kotlin spring-boot

以下是一个最小的示例,显示了我的问题。

Main.kt:

package com.mycompany.configurationpropertiestest

import org.slf4j.LoggerFactory
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.ConstructorBinding
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.runApplication
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service


@SpringBootApplication
@EnableScheduling
@EnableConfigurationProperties(FooServiceConfig::class)
class Application

fun main(args: Array<String>) {
    runApplication<Application>(*args)
}


@ConstructorBinding
@ConfigurationProperties("configurationpropertiestest.foo")
data class FooServiceConfig(
    val interval: Int = 1000,
    val message: String = "hi"
)


@Service
class FooService(
    private val myConfig: FooServiceConfig
) {
    private val log = LoggerFactory.getLogger(this.javaClass)
    //@Scheduled(fixedDelayString = "#{@FooServiceConfig.interval}")
    //@Scheduled(fixedDelayString = "#{@myConfig.interval}")
    @Scheduled(fixedDelayString = "\${configurationpropertiestest.foo.interval}")
    fun talk() {
        log.info(myConfig.message)
    }
}
Run Code Online (Sandbox Code Playgroud)

@ConstructorBinding用于允许拥有FooServiceConfig不可变的成员。)

application.yml:

configurationpropertiestest:
  foo:
    interval: 500
    message: "hi"
Run Code Online (Sandbox Code Playgroud)

Test.kt:

package com.mycompany.configurationpropertiestest

import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner


@RunWith(SpringRunner::class)
@SpringBootTest
class Test {
    @Test
    fun `sit and wait`() {
        Thread.sleep(3000)
    }
}
Run Code Online (Sandbox Code Playgroud)

它有效,但仅有效,因为我interval@Scheduled注释中引用如下:

@Scheduled(fixedDelayString = "\${configurationpropertiestest.foo.interval}")
Run Code Online (Sandbox Code Playgroud)

这在某种程度上破坏了我的服务的良好隔离配置。它突然必须了解外部事物,这是它现在应该了解的。

理想情况下,它只能通过 bean 的类型访问其配置:

@Scheduled(fixedDelayString = "#{@FooServiceConfig.interval}")
Run Code Online (Sandbox Code Playgroud)

或通过注入的实例:

@Scheduled(fixedDelayString = "#{@myConfig.interval}")
Run Code Online (Sandbox Code Playgroud)

但这些尝试分别导致No bean named 'FooServiceConfig' availableNo bean named 'myConfig' available

知道如何实现仅访问配置 bean 而不是全局配置值吗?

Maf*_*for 12

如果您不介意公开FooService.myConfig,这应该可行:

@Service
class FooService(val myConfig: FooServiceConfig) {

    val log = LoggerFactory.getLogger(this.javaClass)

    @Scheduled(fixedDelayString = "#{@fooService.myConfig.interval}")
    fun talk() {
        log.info(myConfig.message)
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:

显然Spring将用注释注释的bean的名称更改@ConstructorBinding[configuration-properties-value]-[fully-qualified-bean-name]. FooServiceConfig最终成为 configurationpropertiestest.foo-com.mycompany.configurationpropertiestest.FooServiceConfig

因此,尽管很丑陋,但这应该也有效:

@Service
class FooService(private val myConfig: FooServiceConfig) {

    val log = LoggerFactory.getLogger(this.javaClass)

    @Scheduled(fixedDelayString = "#{@'configurationpropertiestest.foo-com.mycompany.configurationpropertiestest.FooServiceConfig'.interval}")
    fun talk() {
        log.info(myConfig.message)
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,最后一个选项,回答标题问题:如何在 SpEL 中按类型引用 bean?您可以通过致电beanFactory.getBean

@Service
class FooService(private val myConfig: FooServiceConfig) {

    val log = LoggerFactory.getLogger(this.javaClass)

    @Scheduled(fixedDelayString = "#{beanFactory.getBean(T(com.mycompany.configurationpropertiestest.FooServiceConfig)).interval}")
    fun talk() {
        log.info(myConfig.message)
    }
}
Run Code Online (Sandbox Code Playgroud)