简单实现Aop和注解配合使用

发布时间 2024-01-10 15:52:17作者: 码小白很犇

简单实现Aop和注解配合使用

添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

创建注解

package com.dem.ceshiDemo.annotation;

import java.lang.annotation.*;

/**
 * @author lichangben
 * @date 2021/8/23
 */

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {
    String tag();
    String value() default "";

}

创建需要织入的代码
::: tip 提示
本次置入的代码效果是 将注解的value值放到User对象的用户名里
测试前提:注解放在方法上,且方法必须存在参数User在参数第一个位置
:::

package com.dem.ceshiDemo.aspect;

import com.dem.ceshiDemo.annotation.MyAnnotation;
import com.dem.ceshiDemo.bean.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

/**
 * @author lichangben
 * @date 2021/8/23
 */
@Aspect
@Component
public class MyAspect {

    //切入点
    @Pointcut("@annotation(com.dem.ceshiDemo.annotation.MyAnnotation)")
    public void MyPointCut(){
    }

    //在切入点之前触发
    @Before("MyPointCut()")
    public void doBefore(JoinPoint point) {
        MethodSignature signature = (MethodSignature) point.getSignature();
        //获取获取方法上的注解 注解所在方法:signature.getMethod()
        MyAnnotation annotation = AnnotationUtils.findAnnotation(signature.getMethod(), MyAnnotation.class);
        String value = annotation.value();
        String tag = annotation.tag();
        System.out.println(value);
        System.out.println(tag);
        //获取注解所在方法的参数
        User user = (User) point.getArgs()[0];
        user.setUsername(value);
    }

}

实体类(User)

package com.dem.ceshiDemo.bean;


/**
 * @author lichangben
 * @date 2021/8/23
 */
public class User {
    private String id;
    private String username;

    private String password;
    private int age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    public String   getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

------开始测试------

Controller

package com.dem.ceshiDemo.controller;

import com.dem.ceshiDemo.bean.User;
import com.dem.ceshiDemo.service.CeshiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author lichangben
 * @date 2021/8
 */
@Controller
public class CompressController {

    @Autowired
    private CeshiService ceshiService;

    @GetMapping("/myAspect")
    @ResponseBody
    public User getUserName(){
        User user = new User();
        return ceshiService.getUser(user);
    }


}

service

package com.dem.ceshiDemo.service;

import com.dem.ceshiDemo.annotation.MyAnnotation;
import com.dem.ceshiDemo.bean.User;
import org.springframework.stereotype.Service;

/**
 * @author lichangben
 * @date 2021/8/23
 */
@Service
public class CeshiService {

    @MyAnnotation(value = "lcb",tag = "注解")
    public User getUser(User user){
        return user;
    }
}

结果

::: tip 提示
没有织入代码的话 username结果应该null
:::
结果

可以使用这个做日志