<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.12.RELEASE</version> </dependency>
package com.lei.study_09_19.bean; import com.lei.study_09_19.anno.MyAnno; import org.aspectj.lang.annotation.Aspect; /** * * * @author LeiLei * @date 2019/9/19 */ public class Calc { public int div(int a,int b) { System.out.println(a/b); return a/b; } }
package com.lei.study_09_19.aop; import com.lei.study_09_19.anno.MyAnno; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import java.util.Arrays; import java.util.Collections; /** * * * @author LeiLei * @date 2019/9/19 */ @Aspect public class CalcAop { @Pointcut("execution(public int com.lei.study_09_19.bean.Calc.*(..))") public void pointCut() { } @Before("pointCut()") public void calcBefore() { System.out.println("我在calc之前干了事情!!!"); } @After("pointCut()") public void calcAfter() { System.out.println("我在calc之后干了事情!!!"); } @AfterReturning(value = "pointCut()",returning = "res") public void calcAfter(JoinPoint joinPoint,int res) { System.out.println("你的参数我都知道:"+ Arrays.toString(joinPoint.getArgs())); System.out.println("我知道了你的返回值:"+res); } @AfterThrowing(value = "pointCut()",throwing = "exception") public void calcAfter(JoinPoint joinPoint,Exception exception) { System.out.println("你的方法名我都知道:"+joinPoint.getSignature().getName()); System.out.println("你的异常我都知道:"+ exception); } }
package com.lei.study_09_19.config; import com.lei.study_09_19.aop.AnnoAop; import com.lei.study_09_19.aop.CalcAop; import com.lei.study_09_19.bean.Calc; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.EmbeddedValueResolverAware; import org.springframework.context.annotation.*; import org.springframework.util.StringValueResolver; /** * * * @author LeiLei * @date 2019/9/16 */ @EnableAspectJAutoProxy @Configuration public class BeanConfig { @Bean public Calc calc() { return new Calc(); } @Bean public CalcAop calcAop() { return new CalcAop(); } }
测试结果:
我在calc之前干了事情!!!
4
我在calc之后干了事情!!!
你的参数我都知道:[8, 2]
我知道了你的返回值:4
package com.lei.study_09_19.aop; import com.lei.study_09_19.anno.MyAnno; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import java.lang.reflect.Method; import java.util.Arrays; /** * * * @author LeiLei * @date 2019/9/19 */ @Aspect public class AnnoAop { @Pointcut("@annotation(com.lei.study_09_19.anno.MyAnno)") public void pointCut() { } /** * String toString(); //连接点所在位置的相关信息 * String toShortString(); //连接点所在位置的简短相关信息 * String toLongString(); //连接点所在位置的全部相关信息 * Object getThis(); //返回AOP代理对象 * Object getTarget(); //返回目标对象 * Object[] getArgs(); //返回被通知方法参数列表 * Signature getSignature(); //返回当前连接点签名 * SourceLocation getSourceLocation();//返回连接点方法所在类文件中的位置 * String getKind(); //连接点类型 * StaticPart getStaticPart(); //返回连接点静态部分 * @param joinPoint */ @Before("pointCut()") public void calcBefore(JoinPoint joinPoint) throws Exception { // String aa = joinPoint.getKind();//method-execution // Object aa = joinPoint.getTarget();//com.lei.study_09_19.bean.Calc@10163d6 // String aa = joinPoint.getSignature().getDeclaringTypeName();//com.lei.study_09_19.bean.Calc Class clzz = joinPoint.getSignature().getDeclaringType();//class com.lei.study_09_19.bean.Calc String aa = joinPoint.getSignature().getName();//bbb Method m = clzz.getMethod(aa); MyAnno anno = m.getAnnotation(MyAnno.class); String annoName = anno.name(); System.out.println("我发现你了:"+joinPoint.getSignature().getName()+"|||"+annoName); } }
总结:
1.@EnableAspectJAutoProxy开启注解
2.@Aspect声明切面,写在类上
3..@PonitCut写切入点
2.@Before、@After写前、后、环绕等通知
3.JoinPoint只能放在第一个参数