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"
);
}
本文介绍了如何在Spring框架中引入并使用Properties配置文件。主要内容包括:1) 在application.xml中引入properties文件的方法;2) 如何创建Properties对象并将其注入到Spring的IOC容器中。
9672

被折叠的 条评论
为什么被折叠?



