spring配置文件,需要关注如下两个类。
1 2
| org.springframework.beans.factory.config.PropertiesFactoryBean org.springframework.context.support.PropertySourcesPlaceholderConfigurer
|
PropertiesFactoryBean类图
PropertySourcesPlaceholderConfigurer
如下是配置方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <context:property-placeholder location="classpath*:config/*.properties"/>
<util:properties id="appConfig1" location="classpath*:config/*.properties"/>
<bean id="spc" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config/data.properties</value> <value>classpath:config/data1.properties</value> </list> </property> <property name="ignoreResourceNotFound" value="false"/> </bean>
<bean id="appConfig" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:config/data.properties</value> <value>classpath:config/data1.properties</value> </list> </property> </bean>
|
使用方式上,用@Value注解
PropertiesFactoryBean用#{}的方式
PropertySourcesPlaceholderConfigurer用${}的方式
1 2 3 4 5 6 7 8 9 10
| @Value(value = "${driveClass}") private String driveClass; @Value("${url}") private String url;
@Value("#{appConfig['userName']}") private String userName;
@Value("#{appConfig['config1']}") private String password;
|