Spring的框架中orgspringframewonfigPropertyPlaceholderConfigurer类可以将properties(key/value形式)文件中一些动态设定的值(value)在XML中替换为占位该键($key$)的值properties文件可以根据客户需求自定义一些相关的参数这样的设计可提供程序的灵活性
在Spring中使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件当然也可以指定外部文件的编码如
<bean id=propertyConfigurer class=orgspringframewonfigPropertyPlaceholderConfigurer>
<property name=location>
<value>conf/sqlmap/jdbcproperties</value>
</property>
<property name=fileEncoding>
<value>UTF</value>
</property>
</bean>
当然也可以引入多个属性文件如
<bean id=propertyConfigurer class=orgspringframewonfigPropertyPlaceholderConfigurer>
<property name=locations>
<list>
<value>/WEBINF/mailproperties</value>
<value>classpath: conf/sqlmap/jdbcproperties</value>//注意这两种value值的写法
</list>
</property>
</bean>
基本的使用方法是
Xml代码
<bean id=propertyConfigurerForAnalysis class=orgspringframewonfigPropertyPlaceholderConfigurer>
<property name=location>
<value>classpath:/spring/include/dbQueryproperties</value>
</property>
<property name=fileEncoding>
<value>UTF</value>
</property>
</bean>
其中classpath是引用src目录下的文件写法
当存在多个Properties文件时配置就需使用locations了
Xml代码
<bean id=propertyConfigurer class=orgspringframewonfigPropertyPlaceholderConfigurer>
<property name=locations>
<list>
<value>classpath:/spring/include/jdbcparmsproperties</value>
<value>classpath:/spring/include/baseconfigproperties</value>
<value>classpath*:config/jdbcproperties</value>
</list>
</property>
</bean>
接下来我们要使用多个PropertyPlaceholderConfigurer来分散配置达到整合多工程下的多个分散的Properties文件其配置如下
Xml代码
<bean id=propertyConfigurerForProject class=orgspringframewonfigPropertyPlaceholderConfigurer>
<property name=order value= />
<property name=ignoreUnresolvablePlaceholders value=true />
<property name=location>
<value>classpath:/spring/include/dbQueryproperties</value>
</property>
</bean>
Xml代码
<bean id=propertyConfigurerForProject class=orgspringframewonfigPropertyPlaceholderConfigurer>
<property name=order value= />
<property name=ignoreUnresolvablePlaceholders value=true />
<property name=locations>
<list>
<value>classpath:/spring/include/jdbcparmsproperties</value>
<value>classpath:/spring/include/baseconfigproperties</value>
</list>
</property>
</bean> 其中order属性代表其加载顺序而ignoreUnresolvablePlaceholders为是否忽略不可解析的Placeholder如配置了多个PropertyPlaceholderConfigurer则需设置为true
譬如jdbcproperties的内容为
jdbcdriverClassName=commysqljdbcDriver
jdbcurl=jdbc:mysql://localhost/mysqldb?useUnicode=true&characterEncoding=UTF&zeroDateTimeBehavior=round;
jdbcusername=root
jdbcpassword=
那么在spring配置文件中我们就可以这样写
<bean id=propertyConfigurer class=orgspringframewonfigPropertyPlaceholderConfigurer>
<property name=locations>
<list>
<value>classpath: conf/sqlmap/jdbcproperties </value>
</list>
</property>
</bean>
<bean id=dataSource class=monsdbcpBasicDataSource destroymethod=close>
<property name=driverClassName value=${jdbcdriverClassName} />
<property name=url value=${jdbcurl} />
<property name=username value=${jdbcusername} />
<property name=password value=${jdbcpassword} />
</bean>
这样一个简单的数据源就设置完毕了可以看出PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具
Java代码
<! dataSource >
<bean id=dataSource
class=orgspringframeworkjdbcdatasourceDriverManagerDataSource>
<property name=driverClassName
value=${jdbcdriverClassName} />
<property name=url value=${jdbcurl} />
<property name=username value=${jdbcusername} />
<property name=password value=${jdbcpassword} />
</bean>
<! sessionFactory >
<bean id=sessionFactory
class=orgspringframeworkormhibernateLocalSessionFactoryBean>
<property name=dataSource ref=dataSource />
<property name=mappingResources>
<list>
<value>cn/xg/hibernate/spring/Userhbmxml</value><!这里的映射路径问题这种方法只能一个一个加>
<value>cn/xg/hibernate/spring/Grouphbmxml</value>
</list>
<! 加载一个路径下的*hbmxml文件方法
<property name=mappingDirectoryLocations>
<list>
<value>classpath:/cn/xg/spring/model</value>
</list>
</property>
>
</property>
<property name=hibernateProperties>
<props>
<prop key=hibernatedialect>
${hibernatedialect}
</prop>
<prop key=hibernateshow_sql>true</prop>
</props>
</property>
</bean>
<! DAO实现类extends HibernateDaoSupport注入sessionFactory >
<bean id=userMgrImpl class=cnxghibernatespringUserMgrImpl>
<property name=sessionFactory ref=sessionFactory />
</bean>
<bean id=groupMgrImpl
class=cnxghibernatespringGroupMgrImpl>
<property name=sessionFactory ref=sessionFactory />
<property name=userImpl ref=userMgrImpl/>
<property name=transactionTemplate ref=transactionTemplate/>
</bean>
<! 事务管理 >
<bean id=transactionManager
class=orgspringframeworkormhibernateHibernateTransactionManager>
<property name=sessionFactory ref=sessionFactory />
</bean>
<! 编程式事务的写法 :向Dao实现类中注入transactionTemplate调动其execute()方法接口回调new TransactionCallback()>
<bean id=transactionTemplate class=orgspringframeworktransactionsupportTransactionTemplate>
<property name=transactionManager ref=transactionManager/>
</bean>
<! 声时式事务第一种写法 >
<!
<bean id=groupMgr
class=orgspringframeworktransactioninterceptorTransactionProxyFactoryBean>
<property name=transactionManager ref=transactionManager />
<property name=target ref=groupMgrImpl />
<property name=transactionAttributes>
<props>
<prop key=add*>PROPAGATION_REQUIRED</prop>
<prop key=get*>PROPAGATION_REQUIRED</prop>
<prop key=*>readOnly</prop>
</props>
</property>
</bean>
>
<! 声时式事务第二种写法 >
<! 事务的传播特性
<tx:advice id=txAdvice>
<tx:attributes>
<tx:method name=add* propagation=REQUIRED />
<tx:method name=get* propagation=REQUIRED />
<tx:method name=* readonly=true />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut=execution(* cnxghibernatespring**())
adviceref=txAdvice />
</aop:config>
>
</beans>
jdbcproperties
jdbcdriverClassName=commysqljdbcDriver
jdbcurl=jdbc:mysql://localhost:/数据库名
jdbcusername=数据库用户名
jdbcpassword=数据库密码
hibernatedialect=orghibernatedialectMySQLDialect(方言这里是MySql)