(1)配置文件里加: <context:property-placeholder ignore-resource-not-found="true" location="classpath:datasource.properties, classpath:xxx.properties, classpath:yyyy.properties />
(2)在@Component的类里,直接用${xxx}的形式使用properties文件里的变量即可 @Value("${excelDownPath}") private String excelDownPath; ================================================================ 关于context:property-placeholder与PropertyPlaceholderConfigurer的区别,
应该是建议使用前者吧。
从Spring 3.1.0开始, PropertySourcesPlaceholderConfigurer代替了PropertyPlaceholderConfigurer, 它们配置的写法都是<context:property-placeholder。 ================================================================== http://sethuramanmurali.wordpress.com/2013/05/04/load-properties-file-using-spring-context/
http://java.dzone.com/articles/properties-spring ===================================================================== 第一篇文章复制过来: ==================================================================== One of the easy way to load properties file for spring based application is through spring configuration. It will be difficult too, if we get any issues when loading in such a way.
The usual way to load properties file is through java API. We need to pass the file name and location through system properties or some other way and have to load it. Spring makes it simple in below ways. ------------------------------------------- Prior to Spring 3.1.0 ------------------------------------------- Before spring 3.1.0 release, spring registers a new PropertyPlaceholderConfigurer bean in the Spring Context to access properties. But from 3.1.0, PropertyPlaceholderConfigurer bean no longer registered by spring context and PropertySourcesPlaceholderConfigurer is used instead of that. ----------- Method 1 ----------- Below snippet says manually register bean PropertyPlaceholderConfigurer to access properties. It reads the properties file from classpath. For maven based web applications, once by placing the properties file in src\main\resources directory, Maven places the properties file in WEB-INF\classes directory. Your web application reads the properties from there.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value=classpath:test.properties/> </bean> ----------- Method 2 ----------- The other way is let spring does the bean registration automatically. By using this property-placeholder tag, it registers PropertyPlaceholderConfigurer automatically by spring context.
<context:property-placeholder location="classpath:test.properties" /> You can include more than one properties file like below
<context:property-placeholder location="classpath:test1.properties,classpath:test2.properties" /> property-placeholder tag has some properties like ignore-unresolvable, override mode etc. Please check spring tutorial for that. ----------- Method 3 ----------- You can also write your own bean class by extending PropertyPlaceholderConfigurer and setting the properties file in FileSystemResource class.
public class SystemPropertiesConfigurer extends PropertyPlaceholderConfigurer { ..... ..... public void setSystemDefLocation(String systemDefLocation) { this.systemDefLocation = systemDefLocation; String result = System.getProperty(this.systemDefLocation); setSystemPath(result); FileSystemResource fsr = new FileSystemResource(result + "/properties/test.properties"); setLocation(fsr); } ........ ........ }
-------------------------------------- Spring 3.1.0 -------------------------------------- As I said above, in spring 3.1.0, it registers PropertySourcesPlaceholderConfigurer. This replacement class was created be more flexible and to better interact with the newly introduced Environment and PropertySource mechanism. it should be considered the standard for 3.1 applications.
<bean class="org.springframework.beans.factory.config.PropertySourcesPlaceholderConfigurer"> <property name="location" value=classpath:test.properties/> </bean> or
<context:property-placeholder location="classpath:test.properties" />
----------------- Using properties
With @Value annotation, you can use the properties key to get the value from properties file.
For example, to inject a property using the @Value annotation:
@Value( “${jdbc.url}” ) private String jdbcUrl;
Using properties in Spring XML configuration:
<bean id=”dataSource”> <property name=”url” value=”${jdbc.url}” /> </bean>
And lastly, obtaining properties via the new Environment APIs:
@Autowired private Environment env; … dataSource.setUrl(env.getProperty(“jdbc.url”)); --------------- Some properties in property-placeholder
It has some properties ignore-unresolvable, systemPropertiesMode etc which is quite useful to customize your spring application.
In Spring 3.0 and before, the old PropertyPlaceholderConfigurer also attempted to look for properties both in the manually defined sources as well as in the System properties. The lookup precedence was also customizable via the systemPropertiesMode property of the configurer:
never – Never check system properties
fallback (default) – Check system properties if not resolvable in the specified properties files
override – Check system properties first, before trying the specified properties files. This allows system properties to override any other property source.
By default, in Spring 3.1, local properties are search last, after all environment property sources, including property files. This behavior can be overridden via the localOverride property of the PropertySourcesPlaceholderConfigurer, which can be set to true to allow local properties to override file properties.
Troubleshooting issues when loading properties file from spring context
If your application not detects the properties file, check out the below points.
1. Your properties file is available or not in WEB-INF\classes directory.
2. There should be only one way to load properties from context. You should not use more than one for example, if my application should be like below, it will not load the properties file since ServletContextPropertyPlaceholderConfigurer classes is the subclass of PropertyPlaceholderConfigurer class. It means that I am trying to load one properties from one bean, and another properties file from someother bean. It will not work.
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"> <context:property-placeholder location="classpath:test.properties" ignore-unresolvable="true"/>
|