在Spring中使用@PropertySource
有什麼好處?使用Spring PropertySource的優點是什麼?
給定的配置...
@Configuration
@PropertySource("classpath:foo.properties")
public class Config {}
...我們可以訪問Environment
public class Foo {
@Autowire Environment env;
public void bar() {
String foo = env.getProperty("foo");
}
}
這我們已經可以做定期已有系統屬性。即使檔管理系統很容易與系統性能
if (profile1) System.setProperty("foo", "bar")
else System.setProperty("foo", "baz");
...和
System.getProperty("foo"); // also shorter than autowiring an environment
Plus系統性能沒有一些@PropertySource
- 系統屬性中的不足之處是可迭代,
PropertySource
不是 PropertySource
不允許回退屬性 - 和creati自定義PropertySource
至少等於系統屬性的代碼。Environment
和@Autowire
增加彈簧聯軸器