你可以使用Spring Environment
抽象了點。
首先,你需要財產來源添加到您的Java配置文件
@Configuration
@PropertySource("classpath:/com/mypacakge/core-message.properties")
public class AppConfig {
或者,如果您有多個屬性文件
@Configuration
@PropertySources({
@PropertySource("classpath:core-message.properties"),
@PropertySource("classpath:database.properties")
})
public class AppConfig {
添加PropertySourceConfigurer
到Java的配置文件
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
現在我們假設在您的core-message.properties
您有以下數據
message.name=Hello
您可以通過自動裝配Environment
抽象,然後調用env.getProperty()
@Autowired
Environment env;
public void m1(){
String message = env.getProperty("message.name")` // will return Hello
Environment
對象檢索任何豆這個數據提供界面來配置財產來源和解決性能。它爲從各種來源讀取提供了方便:屬性文件,系統環境變量,JVM系統屬性,servlet上下文參數等等,這非常有用。例如:
environment.getSystemProperties().put("message", "Hello");
System.getProperties().put("message", "Hello");
environment.getSystemProperties().get("message"); // retrieve property
environment.getPropertySources() // allows manipulation of Properties objects
Spring Reference Documentation - Environment
可以加載所有屬性1個屬性對象?另請參閱https://stackoverflow.com/questions/3403773/using-multiple-property-files-via-propertyplaceholderconfigurer-in-multiple-pr – user7294900
我加載它,但在服務端我不能定義@value –