【问题标题】:Assign a property value in a Java class-level annotation在 Java 类级别注释中分配属性值
【发布时间】:2019-11-20 07:06:28
【问题描述】:

在我的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{}

关于它如何工作的任何建议?

【问题讨论】:

  • 你试过了吗? contexts = "${myAppContext}"
  • 是的,它不起作用。它返回 ${myAppContext} 而不是它的值。

标签: java spring properties annotations properties-file


【解决方案1】:

试着做一个豆子:

    @Bean
    public static PropertySourcesPlaceholderConfigurer getPropertyConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

使用@Óscar López 的建议:contexts = "${myAppContext}" 该语法应该可以工作,尤其是在属性值是字符串的情况下。

先不带括号试试这个。如果您需要一个数组,您可能需要考虑在 application.properties 中使用逗号分隔值,如果您需要不同的 Collection 类型,甚至可以考虑使用逗号分隔值 contexts = "#{Arrays.asList(\"${theProperty}\")}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 2018-04-23
    • 2010-12-24
    • 2011-03-04
    • 1970-01-01
    相关资源
    最近更新 更多