Spring 导入配置文件的两种方式
2017-10-30 10:12
441 查看
1.在Spring配置文件中引入properties文件
在application.xml中添加
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
scheme
然后通过
<context:property-placeholder
location="db.properties"></context:property-placeholder>
引入配置文件。
2.读取配置文件,生成properties对象,并注入至IOC容器
在application.xml中添加
xmlns:util="http://www.springframework.org/schema/util" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd scheme 注意版本区分
然后通过
<util:properties
id="commonProps"
location="classpath:"
local-override="true"/>
注入到ico容器中。
在使用@Value 注解获取到属性值
@Value("#{sys.test}")
private String test;
@Value("#{sys.test}")
public void setTest(String test){
test = test;
}
@Value("#{sys}")
public void setSysConf(Properties sys){
test= sys.getProperty("test");
}
在application.xml中添加
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
scheme
然后通过
<context:property-placeholder
location="db.properties"></context:property-placeholder>
引入配置文件。
2.读取配置文件,生成properties对象,并注入至IOC容器
在application.xml中添加
xmlns:util="http://www.springframework.org/schema/util" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd scheme 注意版本区分
然后通过
<util:properties
id="commonProps"
location="classpath:"
local-override="true"/>
注入到ico容器中。
在使用@Value 注解获取到属性值
@Value("#{sys.test}")
private String test;
@Value("#{sys.test}")
public void setTest(String test){
test = test;
}
@Value("#{sys}")
public void setSysConf(Properties sys){
test= sys.getProperty("test");
}
相关文章推荐
- Spring-boot中读取config配置文件的两种方式
- spring读取配置文件的两种方式
- spring+hibernate 两种整合方式配置文件的方法
- Spring-boot中读取config配置文件的两种方式
- spring 配置国际化资源文件的两种方式(转)
- Spring学习-03:ApplicationContext加载配置文件的两种方式
- Spring-boot中读取config配置文件的两种方式
- spring 整合hibernate的配置文件的两种方式
- Spring-boot中读取config配置文件的两种方式
- springBoot使用Controller读取配置文件两种方式&读取自定义配置方法
- Spring 读取XML配置文件的两种方式
- spring加载配置文件的两种方式
- Spring Boot 配置文件两种配置方式对比
- Spring配置文件applicationContext.xml的两种拆分方式
- 详解Spring-boot中读取config配置文件的两种方式
- Spring-boot中读取config配置文件的两种方式
- Spring加载配置文件的两种方式
- spring 配置国际化资源文件的两种方式
- spring加载配置文件的 两种方式
- s2sh整合中hibernate两种配置方式中spring配置文件中的不同