Spring自定义@ConditionalOnProperty批注不能将@Alias用于
我想为我的项目创建FeatureFlag注释,以避免代码重复 我创建了一个名为Spring自定义@ConditionalOnProperty批注不能将@Alias用于,spring,spring-boot,kotlin,Spring,Spring Boot,Kotlin,我想为我的项目创建FeatureFlag注释,以避免代码重复 我创建了一个名为FeatureFlag的新注释。我用带有通用前缀foo.features的ConditionalOnProperty注释修饰。我向注释中添加了新字段,即AliasFortheConditionalOnProperty字段。据我所知,下面的代码应该可以工作,但它不能。我还测试了概要文件注释上的别名,这是有效的 import io.kotlintest.shouldBe import org.junit.jupiter.
FeatureFlag
的新注释。我用带有通用前缀foo.features
的ConditionalOnProperty
注释修饰。我向注释中添加了新字段,即AliasFor
theConditionalOnProperty
字段。据我所知,下面的代码应该可以工作,但它不能。我还测试了概要文件
注释上的别名,这是有效的
import io.kotlintest.shouldBe
import org.junit.jupiter.api.Test
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.annotation.AliasFor
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@ConditionalOnProperty(prefix = "foo.features")
annotation class FeatureFlag(
@get:AliasFor(annotation = ConditionalOnProperty::class, value = "name") val feature: String,
@get:AliasFor(annotation = ConditionalOnProperty::class, value = "havingValue") val enabled: String = "true"
)
@SpringBootTest(
properties = ["foo.features.dummy: true"],
classes = [FeatureFlagTest.FeatureFlagTestConfiguration::class]
)
class FeatureFlagTest(private val applicationContext: ApplicationContext) {
@Configuration
class FeatureFlagTestConfiguration {
@Bean
@FeatureFlag(feature = "dummy")
fun positive(): String = "positive"
@Bean
@FeatureFlag(feature = "dummy", enabled = "false")
fun negative(): String = "negative"
}
@Test
fun `test`() {
applicationContext.getBean(String::class.java) shouldBe "positive"
}
}
在运行测试用例时,出现以下错误:
java.lang.IllegalStateException:必须指定@ConditionalOnProperty的name或value属性
(FeatureFlag
注释应包含name
字段的值。)
你能帮忙吗,我做错什么了?这是框架中的错误吗?除了prefix属性外,还需要在
条件属性
注释中定义name或value属性,以使其正常工作
查看此处以查看您正在使用的批注的详细信息:我在
FeatureFlag
批注中对该字段使用别名。