Chinaunix首页 | 论坛 | 博客
  • 博客访问: 527312
  • 博文数量: 128
  • 博客积分: 4000
  • 博客等级: 上校
  • 技术积分: 1345
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-22 21:43
文章分类

全部博文(128)

文章存档

2009年(30)

2008年(98)

我的朋友

分类: Java

2008-07-05 21:25:14

在Spring中,使用PropertyPlaceholderConfigurer和PropertyOverrideConfigurer可以在XML配置文件中加入外部属性文件
但使用这种方式,有一些需要注意的地方

1.首先在主类中,需要使用ClassPathXmlApplicationContext来读取spring配置xml文件
如:

ApplicationContext context = new ClassPathXmlApplicationContext("example4/appcontext.xml");
HelloWorld hw = (HelloWorld)context.getBean("fileHelloWorld");
log.info(hw.getContent());

直接以beanFactory方式,是无法使用PropertyPlaceholderConfigurer或PropertyOverrideConfigurer的
如下方式不行:
Resource resource = new ClassPathResource("example4/appcontext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
HelloWorld hw = (HelloWorld) factory.getBean("fileHelloWorld");
log.info(hw.getContent());

2.PropertyOverrideConfigurer需要考虑bean的名称
如下是正确配置:
appcontext.xml:

    
        
    

    
        ${fileHelloWorld.statusname}
    



      class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
    


helloworld.properties:
fileHelloWorld.statusname=this is status Name;

如果少了${fileHelloWorld.statusname}中少了Bean名称fileHelloWorld,会导致错误发生
这应该是种强制某个配置是属性某个Bean

3.PropertyPlaceholderConfigurer的配置不需要考虑Bean的名称,直接配置就可以了
配置方式和PropertyOverrideConfigurer类似
阅读(1195) | 评论(0) | 转发(0) |
0

上一篇:Struts2初探

下一篇:兴奋、噩耗

给主人留下些什么吧!~~