多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
在终端执行以下命令: ```shell git checkout -b 08 ``` ```xml <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.11</version> </dependency> ``` ```java package net.zhaoxuyang.ioc.aop; import net.zhaoxuyang.ioc.aop.intercept.Advice; public interface Advisor { Advice getAdvice(); } ``` ```java package net.zhaoxuyang.ioc.aop; public interface ClassFilter { boolean matches(Class<?> targetClass); } ``` ```java package net.zhaoxuyang.ioc.aop; import java.lang.reflect.Method; public interface MethodMatcher { boolean matches(Method method, Class<?> targetClass); } ``` ```java package net.zhaoxuyang.ioc.aop; public interface Pointcut { ClassFilter getClassFilter(); MethodMatcher getMethodMatcher(); } ``` ```java package net.zhaoxuyang.ioc.aop; public interface PointcutAdvisor extends Advisor { Pointcut getPointcut(); } ``` ```java package net.zhaoxuyang.ioc.aop; import java.lang.reflect.Method; import java.util.HashSet; import java.util.Set; import org.aspectj.weaver.tools.PointcutExpression; import org.aspectj.weaver.tools.PointcutParser; import org.aspectj.weaver.tools.PointcutPrimitive; import org.aspectj.weaver.tools.ShadowMatch; public class AspectJExpressionPointcut implements Pointcut, ClassFilter, MethodMatcher{ private static final Set<PointcutPrimitive> DEFAULT_SUPPORTED_PRIMITIVES; static { DEFAULT_SUPPORTED_PRIMITIVES = new HashSet<>(10); DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.EXECUTION); DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.ARGS); DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.REFERENCE); DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.THIS); DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.TARGET); DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.WITHIN); DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_ANNOTATION); DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_WITHIN); DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_ARGS); DEFAULT_SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_TARGET); } private PointcutParser pointcutParser; private String expression; private PointcutExpression pointcutExpression; public AspectJExpressionPointcut(){ this(DEFAULT_SUPPORTED_PRIMITIVES); } public AspectJExpressionPointcut(Set<PointcutPrimitive> supportedPrimitives){ pointcutParser = PointcutParser .getPointcutParserSupportingSpecifiedPrimitivesAndUsingContextClassloaderForResolution( supportedPrimitives ); } protected void checkReadyToMatch(){ if(pointcutExpression == null){ pointcutExpression = buildPointcutExpression(); } } @Override public ClassFilter getClassFilter() { return this; } @Override public MethodMatcher getMethodMatcher() { return this; } @Override public boolean matches(Class<?> targetClass) { checkReadyToMatch(); return pointcutExpression.couldMatchJoinPointsInType(targetClass); } @Override public boolean matches(Method method, Class<?> targetClass) { checkReadyToMatch(); ShadowMatch shadowMatch = pointcutExpression.matchesMethodExecution(method); if(shadowMatch.alwaysMatches()){ return true; } else if(shadowMatch.neverMatches()){ return false; } //其他情况不再判断了,见org.springframework.aop.aspectj.RuntimeTestWalker return false; } private PointcutExpression buildPointcutExpression() { return pointcutParser.parsePointcutExpression(expression); } public void setExpression(String expression) { this.expression = expression; } } ``` ```java package net.zhaoxuyang.ioc.aop; import net.zhaoxuyang.ioc.aop.intercept.Advice; public class AspectJExpressionPointcutAdvisor implements PointcutAdvisor { private AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); private Advice advice; public void setAdvice(Advice advice){ this.advice = advice; } public void setExpression(String expression){ this.pointcut.setExpression(expression); } @Override public Pointcut getPointcut() { return pointcut; } @Override public Advice getAdvice() { return advice; } } ``` ```java package net.zhaoxuyang.ioc.aop; import net.zhaoxuyang.ioc.bean.TestService; import net.zhaoxuyang.ioc.bean.TestServiceImpl; import org.junit.Assert; import org.junit.Test; public class AspectJExpressionPointcutTest { @Test public void testClassFilter() throws Exception { String expression = "execution(* net.zhaoxuyang.ioc.*.*(..))"; AspectJExpressionPointcut aspectJExpressionPointcut = new AspectJExpressionPointcut(); aspectJExpressionPointcut.setExpression(expression); boolean matches = aspectJExpressionPointcut.getClassFilter().matches(TestService.class); Assert.assertTrue(matches); } @Test public void testMethodInterceptor() throws Exception { String expression = "execution(* net.zhaoxuyang.ioc.bean.*.*(..))"; AspectJExpressionPointcut aspectJExpressionPointcut = new AspectJExpressionPointcut(); aspectJExpressionPointcut.setExpression(expression); boolean matches = aspectJExpressionPointcut.getMethodMatcher() .matches(TestServiceImpl.class.getDeclaredMethod("echo"),TestService.class); Assert.assertTrue(matches); } } ``` 在终端执行以下命令: ```shell $ git add . $ git commit -m 'pointcut-and-aspectj' ```