简体   繁体   中英

Java Spring @Value annotation, and parameters

So, I have an environment property that is supposed to reflect what environment my app is running in.

@Value("${environment}")
private String environmentName; //This will be set as sandbox, staging, production

I also have a feature flag (feature is called 'superFeature.enable') that should be set based on what environment the app has been deployed in.

//Config file
superFeature.enable.sandbox=true
superFeature.enable.staging=false
superFeature.enable.production=false


//@Service annotated class containing call to configuration

@Value("${superFeature.enable.{environment}:false}")
private boolean isSuperFeatureEnabled;

At the moment, when I run my app in my sandbox environment, isSuperFeatureEnabled is always false. Is my syntax correct for the above code snippet? Is there something I'm missing?

Shouldn't you have put

@Value("${superFeature.enable.{environmentName}:false}")

there? This is just a guess, I'm afraid. It looked like an inconsistency there. But then, I don't know in which context and order these two statements above are executed, so I don't know where which variables are known. I would suggest getting some more information first. Like, what happens if you replace the {environment} with the expected string for your environment, can you get the environment value in your code at that point. I don't know if it is even possible to have this kind of double indirection at all.

应为以下内容(环境也包含在以$开头的占位符语法中):

@Value("${superFeature.enable.${environment}:false}")

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