From 170016605e92d5f0b5cb82e4dc5a8c11b1d0061b Mon Sep 17 00:00:00 2001 From: fancyears <1243093769@qq.com> Date: Sun, 28 Apr 2019 17:40:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E5=86=99=E8=BF=9Bxml=E7=9A=84?= =?UTF-8?q?=E8=87=AA=E5=8A=A8AOP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/log/LoggerWithoutXml.java | 54 +++++++++++++++++++ spring/src/main/resources/Beans.xml | 8 ++- spring/src/test/java/day5/TestAspect.java | 1 + .../test/java/day6/TestAspectWithoutXml.java | 20 +++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 spring/src/main/java/log/LoggerWithoutXml.java create mode 100644 spring/src/test/java/day6/TestAspectWithoutXml.java diff --git a/spring/src/main/java/log/LoggerWithoutXml.java b/spring/src/main/java/log/LoggerWithoutXml.java new file mode 100644 index 0000000..a59ad2d --- /dev/null +++ b/spring/src/main/java/log/LoggerWithoutXml.java @@ -0,0 +1,54 @@ +package log; + +import org.aspectj.lang.annotation.*; + +/** + * @Author: fancyears·milos·malvis + * @Description: + * @Date: Created in 2019/4/28 15:01 + * @Modified By: + */ +@Aspect +public class LoggerWithoutXml { + + /** Following is the definition for a pointcut to select + * all the methods available. So advice will be called + * for all the methods. + */ + @Pointcut("execution(* bean.Parent.*(..))") + private void selectAll(){ + System.out.println("执行selectAll"); + } + /** + * This is the method which I would like to execute + * before a selected method execution. + */ + @Before("selectAll()") + public void beforeAdvice(){ + System.out.println("切点前操作"); + } + /** + * This is the method which I would like to execute + * after a selected method execution. + */ + @After("selectAll()") + public void afterAdvice(){ + System.out.println("切点后操作"); + } + /** + * This is the method which I would like to execute + * when any method returns. + */ + @AfterReturning(pointcut = "selectAll()", returning="retVal") + public void afterReturningAdvice(Object retVal){ + System.out.println("返回值:" + retVal.toString() ); + } + /** + * This is the method which I would like to execute + * if there is an exception raised by any method. + */ + @AfterThrowing(pointcut = "selectAll()", throwing = "ex") + public void AfterThrowingAdvice(IllegalArgumentException ex){ + System.out.println("发生异常: " + ex.toString()); + } +} diff --git a/spring/src/main/resources/Beans.xml b/spring/src/main/resources/Beans.xml index 90b36c8..c9d968c 100644 --- a/spring/src/main/resources/Beans.xml +++ b/spring/src/main/resources/Beans.xml @@ -10,6 +10,8 @@ > + + @@ -28,7 +30,11 @@ - + + + + +