Spring中bean的属性值一般使用xml配置方式注入,比properties配置要复杂一些,有时候宁愿用properties来配置一些值,故简单介绍一下如何在Spring中使用properties配置
参考文档:http://www.blogjava.net/wmcoo/articles/333345.html
1.在项目中新增一个配置数据类ServiceConfig,通过Annotation方式注入配置
@Configuration
public class ServiceConfig {
@Value("#{configProperties.rootPath}") private String rootPath;
/**
* @return the rootPath
*/
public String getRootPath() {
return rootPath;
}
}
因为配置中已经有<context:component-scan base-package="com.XXX.service" />,所以无需配置<context:annotation-config/>
2,在resource目录下新建一个properties文件,内容如下:
rootPath=/Storage
3,在容器的xml配置文件中增加如下
<util:properties id="configProperties" location="classpath:serivce-config.properties"/>
为了能识别util:properties标签,需要保证xml文件中有如下内容
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
4,通过这种方式需要引入cglib包
cglib的下载地址 http://sourceforge.net/projects/cglib/files/