在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
增加弹簧联轴器