切面编程 = 连接点 + 方位点 + 横切逻辑
1. 横切逻辑
public class LogUtil {
public void beforeMethod(JoinPoint joinPoint){
System.out.println("方法执行之前执行......");
Object[] args = joinPoint.getArgs();
for(Object arg:args){
System.out.println(arg);
}
System.out.println("方法执行之前执行......");
}
public void afterMethod(){
System.out.println("方法执行之后执行......");
}
public void exceptionMethod(){
System.out.println("异常方法执行之后执行......");
}
public void finallyMethod(){
System.out.println("finally执行......");
}
public Object surroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("事务开始!!!");
Object returnValue = null;
try{
returnValue = joinPoint.proceed();
} catch (Exception throwable) {
throwable.printStackTrace();
System.out.println("事务回滚!!!");
throw throwable;
}
System.out.println("事务提交!!!");
return returnValue;
}
}
2. xml配置
<!--横切逻辑-->
<bean id="logUtil" class="deppon.aspectjweaver.LogUtil"></bean>
<!--配置切面-->
<aop:config>
<!--引用横切逻辑-->
<aop:aspect id="logAspect" ref="logUtil">
<!--配置切入点-->
<aop:pointcut id="log1" expression="execution(public void deppon.service.AccountServiceImpl.transfer(java.lang.String,java.lang.String,int))"/>
<!--方位点定位方法,引用切入点-->
<aop:around method="surroundMethod" pointcut-ref="log1"></aop:around>
</aop:aspect>
</aop:config>