spring set Profile from external properties file

146 Views Asked by At

I am setting my database properties from an external file and am trying to do the same for the active profile, but am not having any luck.

In app.properties if tried: spring.config.location= C:\\run\\secrets\\test but that did not work.

In the same config file that I have the database properties being read in I've tried:

@Component
@Configuration
@PropertySource(value={"file:/run/secrets/file.properties"}, ignoreResourceNotFound = true)
public class AppProperties {

 @Resource
    private Environment env;

@Bean
    public Properties props(){
        Properties props = new Properties();
        props.setProperty("spring.profiles.default", env.getRequiredProperty("spring.profiles.active"));
        //props.put("spring.profiles.active", env.getRequiredProperty("spring.profiles.active"));

        return props;
    }
}

In my external file I've tried both spring.profiles.active=dev and spring.profiles.default=dev

Nothing seems to work.

1

There are 1 best solutions below

0
On

If I understand correctly, your file is on Disk C.

So, in windows, the external file path should be like this:

@PropertySource(value={"file:///C:/run/secrets/file.properties"}, ignoreResourceNotFound = true)