【转】Spring配置文件的一般写法(集成hibernate)

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<!-- 单一属性(配置properties文件.) -->
<bean id="propertyConfigurer"
   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location" value="jdbc.properties" /><!-- 注意这里的路径问题,这里是放在src下. -->
</bean>
<!-- 多个属性
   <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="locations">
   <list>
   <value>WEB-INF/mail.properties</value>
   <value>WEB-INF/jdbc.properties</value>
   </list>
   </property>
   </bean>
-->
<!-- dataSource -->
<bean id="dataSource"
   class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName"
    value="${jdbc.driverClassName}" />
   <property name="url" value="${jdbc.url}" />
   <property name="username" value="${jdbc.username}" />
   <property name="password" value="${jdbc.password}" />
</bean>
<!-- sessionFactory -->
<bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource" ref="dataSource" />
   <property name="mappingResources">
    <list>
     <value>cn/xg/hibernate/spring/User.hbm.xml</value><!--这里的映射路径问题,这种方法只能一个一个加-->
     <value>cn/xg/hibernate/spring/Group.hbm.xml</value>
    </list>
    <!-- 加载一个路径下的*.hbm.xml文件方法:
     <property name="mappingDirectoryLocations">
     <list>
     <value>classpath:/cn/xg/spring/model</value>
     </list>
     </property>
    -->
   </property>
   <property name="hibernateProperties">
    <props>
     <prop key="hibernate.dialect">
      ${hibernate.dialect}
     </prop>
     <prop key="hibernate.show_sql">true</prop>
    </props>
   </property>
</bean>
<!-- DAO实现类extends HibernateDaoSupport,注入sessionFactory -->
<bean id="userMgrImpl" class="cn.xg.hibernate.spring.UserMgrImpl">
   <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="groupMgrImpl"
   class="cn.xg.hibernate.spring.GroupMgrImpl">
   <property name="sessionFactory" ref="sessionFactory" />
   <property name="userImpl" ref="userMgrImpl"/>
   <property name="transactionTemplate" ref="transactionTemplate"/>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager"
   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 编程式事务的写法 :向Dao实现类中注入transactionTemplate,调动其execute()方法,接口回调new TransactionCallback()-->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
   <property name="transactionManager" ref="transactionManager"/>
</bean>


<!-- 声时式事务第一种写法 -->
<!--
   <bean id="groupMgr"
   class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
   <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="*" read-only="true" />
   </tx:attributes>
</tx:advice>
<aop:config>
   <aop:advisor pointcut="execution(* cn.xg.hibernate.spring.*.*(..))"
    advice-ref="txAdvice" />
</aop:config>
-->

</beans>

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名
jdbc.username=数据库用户名
jdbc.password=数据库密码
hibernate.dialect=org.hibernate.dialect.MySQLDialect(方言.这里是MySql)

相关推荐