spring的好处--PropertyPlaceholderConfigurer

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

我的程序终于又进展了,基本功能已经实现了,开始开发WEB界面了。这是却出现了一个问题。原先的Encoder需要指定一个配置文件目录,然后将这个目录下的所有文件读入。

<bean id="encoder" class="test.Encoder" init-method="init">

<property name="resourceDirectory"><value>WEB-INF/lib/wbxml</value></property>

</bean>

当在命令行下调试时他工作的很好,但是当在tomcat下运行时却报告说找不到该目录。这是当然,因为tomcat下相对目录不在正确。通常情况下的解决办法是用ServletContext.getRealPath得到绝对路径。这是这样Encoder却额外的需要增加对ServletContext的依赖,这是我所不愿意看到的。

幸好发现了PropertyPlaceholderConfigurer,最后的解决方案是

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />

<bean id="encoder" class="test.Encoder" init-method="init">

<property name="resourceDirectory"><value>${webapp.root}/WEB-INF/lib/wbxml</value></property>

</bean>

原来PropertyPlaceholderConfigurer可以将${...}替换为指定的properties文件或system properties中的值,而spring在Log4jConfigListener或是WebAppRootListener会指定一个system property表示web root的值,default是webapp.root。

不幸的是目前spring1.0.2似乎有一个BUG,PropertyPlaceholderConfigurer不能正确地工作在Inner Bean中,我已提交了这个问题,希望很快能解决。

http://www.springframework.org/docs/api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html

http://www.springframework.org/docs/api/org/springframework/web/util/WebAppRootListener.html

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航