spring怎么加载外部配置文件
在Spring中,可以使用PropertyPlaceholderConfigurer
或PropertySourcesPlaceholderConfigurer
来加载外部配置文件。
- 使用
PropertyPlaceholderConfigurer
加载外部配置文件:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config.properties"/>
</bean>
其中,config.properties
是外部配置文件的路径。
- 使用
PropertySourcesPlaceholderConfigurer
加载外部配置文件:
@Configuration
@PropertySource("classpath:config.properties")
public class AppConfig {
// ...
}
在这种方式下,需要在Spring的Java配置类上使用@PropertySource
注解,并指定外部配置文件的路径。
在两种方式下,外部配置文件中的属性可以通过@Value
注解或Environment
对象进行注入和访问。
@Value("${property.key}")
private String propertyValue;
@Autowired
private Environment env;
public void someMethod() {
String propertyValue = env.getProperty("property.key");
}
版权声明
本文仅代表作者观点,不代表米安网络立场。
上一篇:python如何将循环得到的数据放入列表 下一篇:julia编程语言能做什么
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。