2

我希望能够application.properties在类路径之外放置一个(例如 on d:/),并在那里定义spring.profile.active=production.

如果这被激活,spring 应该另外从类路径加载一个名为my-production.properties.

我尝试了以下方法,但没有奏效。我可能忘记了什么?

@Component
@PropertySources({
    @PropertySource("classpath:my-default.properties"),
    @PropertySource(value = "file:D:/my.properties"),
    @PropertySource(value = "classpath:my-${spring.profiles.active}.properties", ignoreResourceNotFound = true)
})

d:\my.properties:

spring.profiles.active=production

我的默认属性:

testkey=default

我的生产属性:

testkey=production

应用程序:

@Configuration
@EnableAutoConfiguration
public class AppCfg {

    @Value("${testkey}")
    private String testkey;

    @PostConstruct
    public void init() {
        Sysout(testkey); //prints: "default" instead of "production"
    }
}
4

1 回答 1

3

If you want to put applciation properties in other location you can use command line arguments or enviroment variable. See section 21.2 Application property files http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/

If you just want to set the active profile, take a look at section 21. Externalized Configuration You can override the active profile property by using OS environment variables for example.

You can set SPRING_CONFIG_NAME and SPRING_CONFIG_LOCATION environment variables to set the location of application.properties manually. Also you can use the /config subdir of the current directory or the current directory to load the application.properties.

于 2014-09-09T14:26:11.297 回答