通常咱们对于给Spring的bean进行使用properties文件的配置,使用以下的方式:java
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:property-placeholder location= "classpath:props/myPropFile.properties" ignore-unresolvable= "true" /> <!-- sample bean --> <bean id= "sampleBean" class= "your.package.SampleClass" > <property name ="myProp" value= "${prop.in.propfile}" /> </bean> </beans>
而后在SampleClass
中实现myProp
的定义和setter。
这样在两个地方写来写去以为略有麻烦,容易产生问题。
在Spring 3.0以上能够用注解的方式略掉spring配置文件中相关的配置了。
配置中加入:spring
<beans xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"> </beans>
再加入一个配置beanspa
<util:properties id="propsConfig" location="classpath:props/config.properties" />
这样,在用到的地方能够直接code
@Value("#{propsConfig['my.config.string']}") private String needToConfig;
配置文件config.properties
中能够写成xml
my.config.string=configuredString
相似的样子。这样省掉了setter,在一处配置与使用,要方便点。string