[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]Spring里注入properties文件的方法

上一篇:[备忘]My97DatePicker控件,选择月份的写法
下一篇:[备忘]多线程,给账户充值的问题

添加日期:2014/10/31 15:22:17 快速返回   返回列表 阅读3796次
(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"/>
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved