Spring -- spring结合aop 进行 tx&aspectj事务管理配置方法

来源:互联网 发布:python 推荐算法实现 编辑:程序博客网 时间:2024/04/30 11:23

1. tx 配置方法, 代码示例

javabean及其映射文件省略,和上篇的一样

CustomerDao.java, dao层接口

public interface CustomerDao {public void insertCustomer(Customer c);public void updateCustomer(Customer c);public List<Customer> findCustomerByName(String name);//批量保存//public void saveCustomers(List<Customer> list);}

CustomerDaoImpl.java ,dao层实现

前面单纯的hibernate配置要获得currentSession需要在hibernate配置文件中配置 hibernate.current_session_context_class属性值设为thread;

单这里并没有配置,这里之所有能得到currentSession是由于在 Service层实现了事务管理, 在事务管理中有加入session到当前线程

public class CustomerDaoImpl implements CustomerDao {private SessionFactory sf ;public void setSf(SessionFactory sf) {this.sf = sf;}public List<Customer> findCustomerByName(String name) {String hql = "from Customer c where c.name = ?";Query q = sf.getCurrentSession().createQuery(hql);q.setString(0, name);return q.list();}public void insertCustomer(Customer c) {sf.getCurrentSession().save(c);}public void updateCustomer(Customer c) {sf.getCurrentSession().update(c);}}

CustomerService.java ,service层接口

public interface CustomerService {public void insertCustomer(Customer c);public void updateCustomer(Customer c);public List<Customer> findCustomerByName(String name);//批量保存public void saveCustomers(List<Customer> list);}

CustomerServiceImpl.java,service层实现

public class CustomerServiceImpl implements CustomerService {//daoprivate CustomerDao dao ;//注入daopublic void setDao(CustomerDao dao) {this.dao = dao;}public List<Customer> findCustomerByName(String name) {return dao.findCustomerByName(name);}public void insertCustomer(Customer c) {dao.insertCustomer(c);}/** * 批量保存, 编程式事务管理 */public void saveCustomers(final List<Customer> list) {for(Customer c : list){this.insertCustomer(c);}}public void updateCustomer(Customer c) {dao.updateCustomer(c);}}

jdbc.properties

jdbc.driverclass=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/springjdbc.username=rootjdbc.password=rootc3p0.pool.size.max=10c3p0.pool.size.min=2c3p0.pool.size.ini=3c3p0.pool.size.increment=2hibernate.dialect=org.hibernate.dialect.MySQL5Dialecthibernate.show_sql=truehibernate.hbm2ddl.auto=none

sh.xml,spring配置文件

<?xml version="1.0"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context"     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.5.xsd                         http://www.springframework.org/schema/context                         http://www.springframework.org/schema/context/spring-context-2.5.xsd                         http://www.springframework.org/schema/tx                         http://www.springframework.org/schema/tx/spring-tx-2.5.xsd                         http://www.springframework.org/schema/aop                         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">    <!-- 指定分散配置的文件的位置 -->    <context:property-placeholder location="classpath:cn/itcast/spring/hibernate/tx25/jdbc.properties" />    <!-- 配置c3p0数据源 -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="driverClass" value="${jdbc.driverclass}" />        <property name="jdbcUrl" value="${jdbc.url}" />        <property name="user" value="${jdbc.username}" />        <property name="password" value="${jdbc.password}" />        <property name="maxPoolSize" value="${c3p0.pool.size.max}" />        <property name="minPoolSize" value="${c3p0.pool.size.min}" />        <property name="initialPoolSize" value="${c3p0.pool.size.ini}" />        <property name="acquireIncrement" value="${c3p0.pool.size.increment}" />    </bean>    <!-- 本地回话工厂bean,spring整合hibernate的核心入口 -->    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <!-- 指定hibernate自身的属性 -->        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">${hibernate.dialect}</prop>                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>            </props>        </property>        <property name="mappingDirectoryLocations">            <list>                <value>classpath:cn/itcast/spring/domain</value>            </list>        </property>    </bean>    <!-- customerDao -->    <bean id="customerDao" class="cn.itcast.spring.hibernate.tx25.CustomerDaoImpl">        <property name="sf" ref="sessionFactory" />    </bean>    <!-- hibernate事务管理器,在service层面上实现事务管理,而且达到平台无关性 -->    <bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>    <!-- customerService -->    <bean id="customerService" class="cn.itcast.spring.hibernate.tx25.CustomerServiceImpl">        <property name="dao" ref="customerDao" />    </bean>        <!-- 事务通知 -->    <tx:advice id="txAdvice" transaction-manager="htm">        <!-- 事务属性 -->        <tx:attributes>            <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"/>            <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>            <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>            <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>            <tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/>                        <tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>            <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>            <tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>                        <tx:method name="*" propagation="SUPPORTS" isolation="DEFAULT" read-only="false"/>        </tx:attributes>    </tx:advice>        <!-- aop配置(配置切入点) -->    <aop:config>        <aop:pointcut id="txPointcut" expression="execution(* *..*Service.*(..))"/>        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />    </aop:config></beans>

App.java 测试代码

public class App {public static void main(String[] args) throws SQLException {ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/spring/hibernate/tx25/sh.xml");CustomerService cs = (CustomerService) ac.getBean("customerService");List<Customer> list = new ArrayList<Customer>();Customer c = null ;for(int i = 0 ; i < 10 ; i++){c = new Customer();if(i == 9){c.setName(null);}else{c.setName("tom" + i);}c.setAge(20 + i);list.add(c);}cs.saveCustomers(list);}}


2. aspectj 配置方法 代码示例

和(1)tx方式相比只有两个文件有变动

CustomerServiceImpl.java, service实现 以加注解的方式

/** * 通过注解方法实现事务管理 */@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)public class CustomerServiceImpl implements CustomerService {//daoprivate CustomerDao dao ;//注入daopublic void setDao(CustomerDao dao) {this.dao = dao;}/** * 只读 */@Transactional(readOnly=true)public List<Customer> findCustomerByName(String name) {return dao.findCustomerByName(name);}public void insertCustomer(Customer c) {dao.insertCustomer(c);}/** * 批量保存, 编程式事务管理 */public void saveCustomers(final List<Customer> list) {for(Customer c : list){this.insertCustomer(c);}}public void updateCustomer(Customer c) {dao.updateCustomer(c);}}

sh.xml 配置文件

<?xml version="1.0"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context"     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.5.xsd                         http://www.springframework.org/schema/context                         http://www.springframework.org/schema/context/spring-context-2.5.xsd                         http://www.springframework.org/schema/tx                         http://www.springframework.org/schema/tx/spring-tx-2.5.xsd                         http://www.springframework.org/schema/aop                         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">    <!-- 指定分散配置的文件的位置 -->    <context:property-placeholder location="classpath:cn/itcast/spring/hibernate/tx25/jdbc.properties" />    <!-- 配置c3p0数据源 -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="driverClass" value="${jdbc.driverclass}" />        <property name="jdbcUrl" value="${jdbc.url}" />        <property name="user" value="${jdbc.username}" />        <property name="password" value="${jdbc.password}" />        <property name="maxPoolSize" value="${c3p0.pool.size.max}" />        <property name="minPoolSize" value="${c3p0.pool.size.min}" />        <property name="initialPoolSize" value="${c3p0.pool.size.ini}" />        <property name="acquireIncrement" value="${c3p0.pool.size.increment}" />    </bean>    <!-- 本地回话工厂bean,spring整合hibernate的核心入口 -->    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <!-- 指定hibernate自身的属性 -->        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">${hibernate.dialect}</prop>                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>            </props>        </property>        <property name="mappingDirectoryLocations">            <list>                <value>classpath:cn/itcast/spring/domain</value>            </list>        </property>    </bean>    <!-- customerDao -->    <bean id="customerDao" class="cn.itcast.spring.hibernate.aspectj.CustomerDaoImpl">        <property name="sf" ref="sessionFactory" />    </bean>    <!-- hibernate事务管理器,在service层面上实现事务管理,而且达到平台无关性 -->    <bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>    <!-- customerService -->    <bean id="customerService" class="cn.itcast.spring.hibernate.aspectj.CustomerServiceImpl">        <property name="dao" ref="customerDao" />    </bean>        <!-- 使用注解驱动 -->    <tx:annotation-driven transaction-manager="htm"/></beans>







 

0 0