package aspectJ;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect//使用切面需要这个注解
@Component
//扫描到这个类
public class MyAspect {
//定义切入点的表达式
@Pointcut("execution(* dao.*.steal*(..))")
//第一个*表示返回类型,dao包名,第二个*表示class,第三个*代表方法,括号表示所有类型的参数
private void myPointCut(){}
//使用一个返回值为void,方法体为空的方法来命名切入点
@Before("myPointCut()")
//前置通知
public void myBefore(JoinPoint joinPoint){
System.out.println("[守护者]发现猴子正在进入果园,警告不要偷东西.."
);
/*System.out.println("目标类是:"+joinPoint.getTarget());
System.out.print(",被织入增强处理的目标方法为:"+joinPoint.getSignature().getName());*/
}
@AfterReturning(value="myPointCut()",returning="rtv")
//如发生异常,不会执行("myPointCut()&&args(name,..)")
public void myAfterReturning(JoinPoint joinPoint,Object rtv){
System.out.println("[守护者]抓住了猴子,守护者审问出了猴子的名字叫"+rtv+".."
);
//System.out.println("被织入增强处理的目标方法为:"+joinPoint.getSignature().getName());
//System.out.println("Return:"+rtv);
}
//@Around("myPointCut()")
public Object myAround(ProceedingJoinPoint pro)
throws Throwable{
System.out.println("环绕开始:执行目标方法之前,模拟开启事物..."
);
Object obj=
pro.proceed();
System.out.println("环绕结束:执行方法之后,模拟事物关闭.."
);
return obj;
}
//@AfterThrowing(value="myPointCut()",throwing="e")//异常通知,捕获异常
public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
System.out.println("异常通知:"+"出错了"+
e.getMessage());
}
//@After("myPointCut()")//最终通知,发生异常仍然执行
public void myAfter(){
System.out.println("最终通知:模拟方法结束后的释放资源..."
);
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-4.3.xsd
http:
//www.springframework.org/schema/aop
http:
//www.springframework.org/schema/aop/spring-aop-4.3.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 指定需要扫描的包,使注解生效 -->
<context:component-scan base-
package="dao" />
<!-- <context:component-scan base-
package="aspectJ" /> -->
<context:component-scan base-
package="entity" />
<!-- 启动基于注解的声明式AspectJ支持 -->
<aop:aspectj-autoproxy />
</beans>
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dao.MonkeyDao;
public class Test {
public static void main(String[] args){
String xmlPath="test/aspects.xml"
;
ApplicationContext applicationContext=
new ClassPathXmlApplicationContext(xmlPath);
MonkeyDao monkeyDao=(MonkeyDao) applicationContext.getBean("monkeyDaoImpl"
);
monkeyDao.stealPeach("孙大圣"
);
monkeyDao.stealApple("孙二圣"
);
monkeyDao.dance();
}
}
转载于:https://www.cnblogs.com/nomouren/p/9919162.html
相关资源:Spring加Aspect的jar包