XML配置:
<!--配置service类-->
<bean id="accountService" class="com.AccountServiceImpl"></bean>
<!--配置通知类-->
<bean id="logger" class="com.Logger"></bean>
<aop:config>
<!--配置表达式变量-->
<aop:pointcut id="pt1" expression="execution(* com.lxwc.service.impl.*.*(..))"/>
<aop:aspect id="logAdvice" ref="logger">
<!--前通知-->
<aop:before method="before" pointcut-ref="pt1"></aop:before>
<!--后通知-->
<aop:after-returning method="returning" pointcut-ref="pt1"></aop:after-returning>
<!--最终通知-->
<aop:after method="after" pointcut-ref="pt1"></aop:after>
<!--异常通知-->
<aop:after-throwing method="throwing" pointcut-ref="pt1"></aop:after-throwing>
</aop:aspect>
<!--环绕通知-->
<aop:around method="around" pointcut-ref="pt1"></aop:around>
</aop:config>
/**
* 环绕通知
* @param p spring提供注入 用于调用原方法
* @return 返回Object
*/
public Object around(ProceedingJoinPoint p){
Object reReturn = null;
try {
System.out.println("环绕通知-前通知");
reReturn = p.proceed();
System.out.println("环绕通知-后通知");
} catch (Throwable throwable) {
System.out.println("环绕通知-异常通知");
throwable.printStackTrace();
}finally {
System.out.println("环绕通知-最终通知");
}
return reReturn;
}
注解配置
xml配置
<!--配置注解扫描的包-->
<context:component-scan base-package="com.lxwc"/>
<!--开始AOP注解支持-->
<aop:aspectj-autoproxy/>
注解代码
@Aspect 表示这个类是个切面类
@Pointcut 切面表达式
@Before 前通知
@AfterReturning 后通知
@AfterThrowing 异常通知
@After 最终通知
@Around 环绕通知
@Component("logger")
@Aspect
public class Logger {
@Pointcut("execution(* com.lxwc.service.impl.*.*(..))")
public void pt1(){};
@Before("pt1()")
public void before(){
System.out.println("打印日志了.......前通知");
}
@AfterReturning("pt1()")
public void returning(){
System.out.println("打印日志了.......后通知");
}
@After("pt1()")
public void after(){
System.out.println("打印日志了.......最终通知");
}
@AfterThrowing("pt1()")
public void throwing(){
System.out.println("打印日志了.......异常通知");
}
@Around("pt1()")//环绕通知
public Object huanrao(ProceedingJoinPoint point){
Object reReturn = null;
try {
System.out.println("打印日志了.......前通知-环绕通知");
reReturn = point.proceed();
System.out.println("打印日志了.......后通知-环绕通知");
} catch (Throwable throwable) {
System.out.println("打印日志了.......异常通知-环绕通知");
throwable.printStackTrace();
}finally {
System.out.println("打印日志了.......最终通知-环绕通知");
}
return reReturn;
}
}
SpringAOP配置事物管理器
<!--配置事物管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事物属性-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!--配]1务的属性
isolation:用于指定事务的隔离级别。默认位是DEFAULT,表示使用数据库的默认隔离级别。
read-only:用于指定事物是否只读。只有查询方法才能设置为true。默认值是false,表示读写。
timeout:用于指定事物的超时时间、默认值是-1、表示永不超时。如果指定了数值,以秒为单位。
propagation:用于指定事物的传播行为。默认值是REQUIRED、表示一定会有事物、增删改的选择。查询方法可以选择SUPPORTS。
rollback-for:用于指定一个异常,当产生该异常时,事物回滚,产生其他异常时,事务不回滚。没有默认值。表示任何异常都回滚。
no-rollback-for:用于指定一个异常,当产生该异常时.事物不回滚,产生其他异常时事物回滚。没有默认值,表示任何异常都回滚-->
<tx:attributes>
<!--配置增删改-->
<tx:method name="transfer" propagation="REQUIRED" read-only="false"/>
<!--配置查询-->
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--配置AOP-->
<aop:config>
<aop:pointcut id="pt1" expression="execution(* com.lxwc.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
网友评论