上一篇用注解方式实现了springAop,现在加上自定义注解。
其实所谓的自定义注解来实现Aop,只不过是在拦截的方法上做一定修改而已,自定义一个注解,拦截拥有自定义注解的方法,仅此而已,如果表达式完全可以达到自己的拦截效果完全没必要自定义注解。
applicationContext.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="annotationAop"></context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean id="testBean" class="controller.logController"></bean> </beans>
自定义注解类
如何自定义注解可以参照https://blog.csdn.net/zhushuai1221/article/details/52181575
package annotationAop; import java.lang.annotation.*; @Documented @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface AnnotationDefinition { }
注解切面类
package annotationAop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; @Component @Aspect public class AspectClass { //将切点换成了自己定义的注解 @Pointcut("@annotation(AnnotationDefinition)") public void pointCut(){ } @Before(value = "pointCut()") public void before(){ System.out.println("注解测试方法执行前"); } @After(value = "pointCut()") public void after(){ System.out.println("注解测试方法执行后"); } @AfterReturning(value = "pointCut()") public void returnMehtod(){ System.out.println("注解测试方法返回后执行"); } @AfterThrowing(value = "pointCut()") public void throwMethod(){ System.out.println("注解测试方法异常尝试后执行"); } @Around(value = "pointCut()") public void aroundMethod(ProceedingJoinPoint joinPoint){ System.out.println("注解测试方法环绕方法执行前"); try{ joinPoint.proceed(); System.out.println("注解测试方法环绕方法执行后"); }catch(Throwable throwable){ throwable.printStackTrace(); } } }
测试类(和上一篇一样)
public class App { public static void main( String[] args ) throws Exception { //加载spring配置文件 AbstractApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); //获取测试用bean logController test = (logController)context.getBean("testBean"); //会被拦截的方法 test.firsttest1(); System.out.println("----------------------"); test.secondtest1(); System.out.println("----------------------"); test.aroundtest1(); System.out.println("----------------------"); test.returntest1(); System.out.println("----------------------"); //test.throwtest1(); System.out.println("----------------------"); //不会被拦截的方法 test.firsttest2(); System.out.println("----------------------"); test.secondtest2(); } }
测试结果
注解测试方法环绕方法执行前
注解测试方法执行前
第一个test1执行中
注解测试方法环绕方法执行后
注解测试方法执行后
注解测试方法返回后执行
----------------------
注解测试方法环绕方法执行前
注解测试方法执行前
第二个test1执行中
注解测试方法环绕方法执行后
注解测试方法执行后
注解测试方法返回后执行
----------------------
环绕方法测试
----------------------
返回值方法测试
----------------------
----------------------
第一个test2执行中
----------------------
第二个test2执行中
可以看到只有添加了自定义注解的方法才进行了增强