Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1900526
  • 博文数量: 606
  • 博客积分: 9991
  • 博客等级: 中将
  • 技术积分: 5725
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-17 19:07
文章分类

全部博文(606)

文章存档

2011年(10)

2010年(67)

2009年(155)

2008年(386)

分类: Java

2009-04-07 14:55:09

1. jdbc.properties

database.url=jdbc:mysql://localhost/smaple
database.driver=org.gjt.mm.mysql.Driver
database.user=root
database.password=star1xing


2.conf.xml


SPRING//DTD BEAN//EN" "spring-beans.dtd">


            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       
            com/starxing/test/jdbc.properties
       

   

            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       
            ${database.url}
       

       
            ${database.driver}
       

       
            ${database.user}
       

       
            ${database.password}
       


   


----------------------------------------------------------------------------------------------
 

关于PropertyPlaceholderConfigurer与PropertyOverrideConfigurer

PropertyPlaceholderConfigurer,允许在spring的配置文件中加入properties文件,可以将一些动态参数移到properties中.

Java代码 复制代码
  1. "propertyConfigurer"  
  2.           class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
  3.         "location" value="classpath:config/jdoserver.properties"/>   
  4.       

但是好像在属性文件定义中却不支持多个属性文件的定义,比如不能这样用config/*.properties

经过查看源码,发现可以使用locations属性定义多个配置文件:

Java代码 复制代码
  1. "locations">   
  2.                
  3.                 classpath:config/maxid.properties   
  4.                 classpath:config/jdoserver.properties   
  5.                
  6.   

 

使用外部属性后如下:

Java代码 复制代码

  1. "dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">   
  2.         "driverClassName" value="${jdbc.agent.driver}"/>   
  3.         "url" value="${jdbc.agent.main.url}"/>   
  4.       

PropertyOverrideConfigurer:在spring所有的bean初使化以后,将bean的值强行改变   

Xml代码 复制代码
  1. <bean id="configBean"    
  2.    class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">    
  3.          <property name="location">    
  4.              <value>hello.propertiesvalue>    
  5.          property>    
  6.      bean>    
  7.   
  8.      <bean id="helloBean" class="com.HelloBean">    
  9.          <property name="word">    
  10.              <value>Hello!value>    
  11.          property>    
  12.      bean>  

定义HelloBean,注入word的值为hello.

 在hello.properties

helloBean.word=Welcome!

 

word初使为hello后,当bean全加载完,PropertyOverrideConfigurer将helloBean.word的值改成为welcome.

阅读(852) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~