有几点需要说明:
⑴ pointcut中的三个"*"中,第一个*代表返回值,第二*代表service下子包,第三个*代表方法名,“(..)”代表方法参数。
⑵ 此时配置的切点在Service层,方法命名需要按照以上advice通知点开头命名。
⑶ 按照规定命名方法,会受到Spring事务的管控,保持操作的一致性。例如向插入100条数据,前面99条记录都正常执行直至第100条出现错误,则事务管控会回滚到执行前的初始状态。
2、使用Bean代理
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.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"
default-autowire="default">
<!-- <bean name="userManager" class="com.tgb.manager.UserManagerImpl"></bean>
<bean name="userController" class="com.tgb.web.UserController">
<property name="userManager" ref="userManager"></property>
</bean> -->
<context:component-scan base-package="com.tgb.dao" />
<context:component-scan base-package="com.tgb.entity" />
<context:component-scan base-package="com.tgb.manager" />
<context:component-scan base-package="com.tgb.web" />
<!-- 引入init.properties中属性 -->
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/spring/jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源,连接池使用c3p0,详细信息参见hibernate官方文档"基础配置章节" -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="driverClass" value="${datasource.driverClassName}"></property>
<property name="jdbcUrl" value="${datasource.url}"></property>
<property name="user" value="${datasource.username}"></property>
<property name="password" value="${datasource.password}"></property>
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">
${hibernate.show_sql}
</prop>
<prop key="hibernate.jdbc.fetch_size">
${hibernate.jdbc.fetch_size}
</prop>
<prop key="hibernate.jdbc.batch_size">
${hibernate.jdbc.batch_size}
</prop>
<prop key="hibernate.connection.release_mode">
${hibernate.connection.release_mode}
</prop>
<prop key="hibernate.format_sql">
${hibernate.format_sql}
</prop>
<prop key="hibernate.connection.SetBigStringTryClob">true</prop>
</props>
</property>
<!-- anotation注解扫描实体类 -->
<property name="annotatedClasses">
<list>
<value>com.tgb.entity.User</value>
</list>
</property>
</bean>
<!-- 配置一个事务管理器 将事务与Hibernate关联-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事务范围,使用代理的方式 -->
<bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<!-- 为事务代理bean注入事务管理器-->
<property name="transactionManager" >
<ref bean="transactionManager"/>
</property>
<!--设置事务属性范围-->
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="get">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-myException</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="userDao" class="com.tgb.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userManagerBase" class="com.tgb.manager.UserManagerImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 此处为代理 -->
<bean name="userManager" parent="transactionProxy">
<property name="target" ref="userManagerBase"></property>
</bean>
</beans>
参考链接:
简单理解——面向切面编程(AOP)
AOP 实现原理
spring事务配置的五种方式
Spring声明式事务配置管理方法
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-64567-3.html
小米公司知道国家质检总局抢不到小米手机后