熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Spring 利用PropertyPlaceholderConfigurer占位符

2022-06-13   來源: Java開源技術 

  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&amp;characterEncoding=UTF&amp;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)


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28898.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.