简体   繁体   中英

Assign a property value in a Java class-level annotation

In my application.properties , I have a property as this:

myAppContext=something.special

The Spring app reads the properties from this file.

I want to access the above property in a class-level annotation as this:

// "contexts" take in an array of string values
@AClassLevelAnnotation(contexts = {"something.special"})
public class Amazing{}

Instead of using the value (which already exists in the properties file) I would like to access it using the property key, something like this, which does not work:

@AClassLevelAnnotation(contexts = {@Value("${myAppContext}")})
public class Amazing{}

Any suggestion on how this can work?

Try making a bean:

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

Using @Óscar López's suggestion: contexts = "${myAppContext}" That syntax should be working, especially if the property value is a string.

Try this without the brackets first. If you need an array, you may want to consider using a comma-separated value in your application.properties, or even something like contexts = "#{Arrays.asList(\\"${theProperty}\\")}" , with the comma-separated value, if you need a different Collection type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM