文接上篇:【spring】AOP切面注解学习(一)
AOP切面注解测试示例代码
示例代码 一
maven的pom文件导入
<dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId></dependency>
AspectDemoController类
package com.yang.SpringTest.annotation.aopLearn.controller;import org.springframework.stereotype.Controller;/*** <p>AspectDemoController类</p>** @author By: chengxuyuanshitang* Package com.yang.SpringTest.annotation.aopLearn* Ceate Time 2024-04-13 16:28*/
@Controller
public class AspectDemoController {public void demo () {System.out.println ("-------- Aspect Test... -----------");}
}
AspectDemoLog类
package com.yang.SpringTest.annotation.aopLearn;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;/*** <p>AspectDemoLog类</p>** @author By: chengxuyuanshitang* Package com.yang.SpringTest.annotation.aopLearn* Ceate Time 2024-04-13 16:31*/
@Aspect
@Component
public class AspectDemoLog {@Pointcut("execution(* com.yang.SpringTest.annotation.aopLearn.controller.*.*(..))")private void pointCut(){}@Before("pointCut()")public void beforeLog(){System.out.println(" ====== @Before Executing...");}@After("pointCut()")public void afterLog(){System.out.println(" ====== @After Executing...");}@AfterReturning("pointCut()")public void afterReturningLog(){System.out.println(" ====== @AfterReturning Executing...");}@AfterThrowing("pointCut()")public void afterThrowingLog(){System.out.println(" ====== @AfterThrowing Executing...");}@Around("pointCut()")public Object aroundLog(ProceedingJoinPoint pjp){Object resultValue = null;try{System.out.println(" ====== @Around Executing Starting... ");Object[] args = pjp.getArgs();resultValue = pjp.proceed(args);System.out.println(" ====== @Around Executing ending... ");}catch (Throwable t){System.out.println(" ====== @Around Executing Exception ... ");}finally {System.out.println(" ====== @Around Executing Finally... ");}return resultValue;}
}
AspectDemoConfig配置类
package com.yang.SpringTest.annotation.aopLearn;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;/*** <p>AspectDemoConfig配置类</p>** @author By: chengxuyuanshitang* Package com.yang.SpringTest.annotation.aopLearn* Ceate Time 2024-04-13 16:41*/
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(value = {"com.yang.SpringTest.annotation.aopLearn"})
public class AspectDemoConfig {
}
AspectDemoTest测试类
package com.yang.SpringTest.annotation.aopLearn;import com.yang.SpringTest.annotation.aopLearn.controller.AspectDemoController;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;/*** <p>AspectDemoTest测试类</p>** @author By: chengxuyuanshitang* Package com.yang.SpringTest.annotation.aopLearn* Ceate Time 2024-04-13 16:42*/
public class AspectDemoTest {public static void main (String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (AspectDemoConfig.class);AspectDemoController aspectDemoController = context.getBean (AspectDemoController.class);aspectDemoController.demo ();context.close ();}
}
运行结果
示例代码 二
测试异常在AspectDemoController类的demo方法添加异常代码。
int num = 1 / 0;