spring的好处--PropertyPlaceholderConfigurer

来源:互联网 发布:apk反编译为源码 编辑:程序博客网 时间:2024/06/09 18:48
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

我的程序终于又进展了,基本功能已经实现了,开始开发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中,我已提交了这个问题,希望很快能解决。

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

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

 

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击