1

I have set the Environment variable JAVA_OPTS (-Dappconfig=D:/cc/dd -Dfarm=test and want to read properties from a property using the following code but no matter what I tried I got the error below. I need to do this for different environments (test, staging,dev , prod). Any help is appreciated

        Caused by:
            java.lang.IllegalArgumentException: Could not resolve placeholder 'appconfig' in string value
 "file:${appconfig}/farm/${farm}/myservice.appconfig.properties"


@Configuration
@PropertySource(value = "file:${appconfig}/farm/${farm}/myservice.appconfig.properties", ignoreResourceNotFound = false)
public class AppConfig
{
    @Autowired
    private Environment environment;


    @Bean
    public AppConfigSettings getAppConfig()
    {
        AppConfigSettings properties = new AppConfigSettings()

         //I set properties using environment.getRequiredProperty("propertykey") //here


        return properties;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer()
    {
        return new PropertySourcesPlaceholderConfigurer();
    }
}
Dan Hunex
  • 5,172
  • 2
  • 27
  • 38

2 Answers2

1

Kudos to this answer

I setup my environment variable as

_JAVA_OPTIONS= -Dfarm=test -Dappconfig=D:\cc\dd

Use _JAVA_OPTIONS instead of JAVA_OPTS

It works perfect now

Community
  • 1
  • 1
Dan Hunex
  • 5,172
  • 2
  • 27
  • 38
-1

A portion of the documentation for the PropertySource annotation has this to say regarding placeholders within @PropertySource resource locations:


Any ${...} placeholders present in a @PropertySource resource location will be resolved against the set of property sources already registered against the environment. For example:

@Configuration
@PropertySource("classpath:/com/${my.placeholder:default/path}/app.properties")
public class AppConfig {
    @Autowired
    Environment env;

    @Bean
    public TestBean testBean() {
        TestBean testBean = new TestBean();
        testBean.setName(env.getProperty("testbean.name"));
        return testBean;
    }
}

Assuming that "my.placeholder" is present in one of the property sources already registered, e.g. system properties or environment variables, the placeholder will be resolved to the corresponding value. If not, then "default/path" will be used as a default. Expressing a default value (delimited by colon ":") is optional. If no default is specified and a property cannot be resolved, an IllegalArgumentException will be thrown.


I am not sure what other @PropertySource annotations you may be using, or if you can even control the order that they are applied, but this seems like it may be the situation you are encountering.

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • from your statement Assuming that "my.placeholder" is present in one of the property sources already registered, e.g. system properties or environment variables. yes I have set the JAVA_OPTS which is environment variable but it is not working and I have seen what you posted on the spring page – Dan Hunex Feb 11 '16 at 07:13