0
我使用Maven創建簡單的Spring應用程序,並擁有2個配置和屬性。該層次是:覆蓋多個彈簧應用程序屬性
- package.main
- App.java
- AppConfig.java
- app.properties
- package.main.model
- ModelConfig.java
- model.properties
App.java
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println(applicationContext.getEnvironment().getActiveProfiles()[0]);
}
AppConfig.java
@Configuration
@Import(ModelConfig.class)
@PropertySource("classpath:/package/main/app.properties")
public class AppConfig {}
app.properties
spring.profiles.active = prod
個
ModelConfig.java
@Configuration
@PropertySource("classpath:/package/main/model/model.properties")
@ComponentScan
public class ModelConfig {}
model.properties
spring.profiles.active = dev
- 爲什麼
model.properties
是覆蓋app.properties
(結果是dev
)? - 如何使
application.properties
像Spring Boot那樣不能被新屬性覆蓋?