Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring自定义@ConditionalOnProperty批注不能将@Alias用于_Spring_Spring Boot_Kotlin - Fatal编程技术网

Spring自定义@ConditionalOnProperty批注不能将@Alias用于

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注释,以避免代码重复

我创建了一个名为
FeatureFlag
的新注释。我用带有通用前缀
foo.features
ConditionalOnProperty
注释修饰。我向注释中添加了新字段,即
AliasFor
the
ConditionalOnProperty
字段。据我所知,下面的代码应该可以工作,但它不能。我还测试了
概要文件
注释上的别名,这是有效的


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
批注中对该字段使用别名。