Spring2.0事务(jdbc/hibernate)

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">


<bean id="conn" class="org.apache.commons.dbcp.BasicDataSource">
   <property name="driverClassName"
    value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
   </property>
   <property name="url"
    value="jdbc:microsoft:sqlserver://localhost:1433">
   </property>
   <property name="username" value="sa"></property>
</bean>
<bean id="sess"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource">
    <ref bean="conn" />
   </property>
   <property name="hibernateProperties">
    <props>
     <prop key="hibernate.dialect">
      org.hibernate.dialect.SQLServerDialect
     </prop>
    </props>
   </property>
   <property name="mappingResources">
    <list>
     <value>po/Admin.hbm.xml</value>
    </list>
   </property>
</bean>

<bean id="hibernateTransactionManager"
   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory">
    <ref bean="sess" />
   </property>
</bean>

<tx:advice transaction-manager="hibernateTransactionManager"
   id="txAdvice">
   <tx:attributes>
    <tx:method name="*" propagation="REQUIRED"></tx:method>
   </tx:attributes>
</tx:advice>

<aop:config>
   <aop:pointcut id="myPoint"
    expression="execution(* com.services..*.*(..))" />
   <aop:advisor advice-ref="txAdvice" pointcut-ref="myPoint" />
</aop:config>

<bean id="adminDAO" class="po.AdminDAO">
   <property name="sessionFactory">
    <ref bean="sess" />
   </property>
</bean>

<bean id="adminService" class="com.services.AdminServices">
   <property name="adminDAO">
    <ref bean="adminDAO" />
   </property>
</bean>
</beans>

===================================================================

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
<aop:aspectj-autoproxy/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
    <property name="url" value="jdbc:microsoft:sqlserver://localhost:1433"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>


<bean name="adminDAO" class="com.test.AdminDAO"
   init-method="init">
   <property name="ds">
    <ref bean="dataSource" />
   </property>
</bean>

<bean name="check" class="com.services.Check">
   <property name="admindao">
    <ref bean="adminDAO" />
   </property>
</bean>

<bean id="txManager"
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource" />
</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">
   <tx:attributes>
    <tx:method name="get*" read-only="true" />
    <tx:method name="*" />
   </tx:attributes>
</tx:advice>

<aop:config>
   <aop:pointcut id="fooServiceOperation"
    expression="execution(* com.*services*.*(..))" />
   <aop:advisor advice-ref="txAdvice"
    pointcut-ref="fooServiceOperation" />
</aop:config>

</beans>

 

 

你可能感兴趣的:(spring,AOP,Hibernate,bean,jdbc)