Spring中使用properties配置

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/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值