## 只记录使用,无概念
一、创建项目
创建springboot项目,勾选web和aop,项目结构如图
二、编写代码
(1)业务逻辑代码,UserService和UserServiceImpl
package com.zjl.aop.service;
public interface UserService {
/**
* 添加
*
* @return int
*/
int add();
/**
* 更新
*
* @return int
*/
int update();
/**
* 删除
*
* @return int
*/
int delete();
/**
* 选择
*
* @return int
*/
void select();
}
package com.zjl.aop.service.impl;
import com.zjl.aop.service.UserService;
import org.springframework.stereotype.Service;
/**
* 用户服务impl
* @author zjl
* @date 2021/06/07
*/
@Service
public class UserServiceImpl implements UserService {
@Override
public int add() {
System.out.println("添加用户方法");
return 0;
}
@Override
public int update() {
System.out.println("修改用户方法");
return 0;
}
@Override
public int delete() {
System.out.println("删除用户方法");
return 10;
}
@Override
public void select() {
System.out.println("查询用户方法");
throw new NullPointerException();
}
}
(2)切面类代码
package com.zjl.aop.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* 日志切面
* 1.Before 前置通知
* 2.After 后置通知
* 3.Around 环绕通知
* 4.AfterReturning 返回通知
* 3.AfterThrowing 异常通知
* @author zjl
* @date 2021/06/07
*/
@Aspect
@Component
public class LogAspect {
@Pointcut("execution(* com.zjl.aop.controller.*.*(..))")
public void controller(){
}
@Before("controller()")
public void before2(JoinPoint point){
String name=point.getSignature().getName();
System.out.println(name+"方法的controller前置通知");
}
@Pointcut("execution(* com.zjl.aop.service.*.*(..))")
public void service(){
}
@Before("service()")
public void before(JoinPoint point){
String name=point.getSignature().getName();
System.out.println(name+"方法的service前置通知");
}
@After("service()")
public void after(JoinPoint point){
String name=point.getSignature().getName();
System.out.println(name+"方法的service后置通知");
}
@Around("execution(* com.zjl.aop.service.*.update(..))")
public int around(ProceedingJoinPoint point) throws Throwable {
String name=point.getSignature().getName();
System.out.println(name+"方法的service前置环绕通知");
point.proceed();
System.out.println(name+"方法的service后置环绕通知");
return 0;
}
@AfterReturning(pointcut = "execution(* com.zjl.aop.service.*.delete(..))",returning = "result")
public void AfterReturning(JoinPoint point,Object result) throws Throwable {
String name=point.getSignature().getName();
System.out.println(name+"方法的service返回通知, 返回结果为: "+result);
}
@AfterThrowing(pointcut = "execution(* com.zjl.aop.service.*.select(..))",throwing = "e")
public void AfterThrowing(JoinPoint point,Throwable e){
String name=point.getSignature().getName();
System.out.println(name+"方法的service异常通知, 异常为: "+e.getMessage());
}
}
(3)测试类代码
package com.zjl.aop;
import com.zjl.aop.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class AopApplicationTests {
@Autowired
private UserService userService;
@Test
void testAdd() {
userService.add();
}
@Test
void testUpdate() {
userService.update();
}
@Test
void testDelete() {
userService.delete();
}
@Test
void testSelect() {
userService.select();
}
}
(4)测试结果