在我的application.properties
中,我有一个属性,如下所示:
myAppContext=something.special
Spring 应用程序从此文件中读取属性。
我想在类级注释中访问上述属性,如下所示:
// "contexts" take in an array of string values
@AClassLevelAnnotation(contexts = {"something.special"})
public class Amazing{}
我想使用属性键来访问它,而不是使用值(属性文件中已经存在),就像这样,但不起作用:
@AClassLevelAnnotation(contexts = {@Value("${myAppContext}")})
public class Amazing{}
关于如何运作有什么建议吗?
最佳答案
尝试制作一个 bean :
@Bean
public static PropertySourcesPlaceholderConfigurer getPropertyConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
使用 @Óscar López 的建议:contexts = "${myAppContext}"
该语法应该有效,特别是当属性值是字符串时。
首先尝试不使用括号。如果您需要一个数组,您可能需要考虑在 application.properties 中使用逗号分隔的值,甚至类似 contexts = "#{Arrays.asList(\"${theProperty}\")}"
,如果您需要不同的集合类型,则使用逗号分隔值。
关于java - 在 Java 类级注释中分配属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56974349/