Aspect切面进行统一参数处理demo

2024-10-09 10:05:54 浏览数 (3)

Aspect切面进行统一参数处理demo

//导入 implementation('org.springframework:spring-aspects:5.3.22')

代码语言:javascript复制
import com.example.mytester.entity.ClassRoom;
import com.example.mytester.entity.Student;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
@Aspect
public class ChangeStudentNameAdvice {

    /**
     * 关键:入参需要保持一致,否则切面无法织入生效。
     * @param student
     * @param request
     */
    @Before("execution(* com.example.mytester.controller.*.*(..))&&args(student,request)")
    public void aroundPoints(Student student, HttpServletRequest request) {
        student.setName("AOP-NAME");
    }

//    @Before("execution(* com.example.mytester.controller.TestController.saveStudent5())&&args(classRoom))")   //这样写不生效
    @Before("execution(* com.example.mytester.controller.*.*(..))&&args(classRoom))")
    public void aroundPoints(ClassRoom classRoom) {
        classRoom.setName("AOP-NAME-2");
    }


}


public class ClassRoom {
    private static final long serialVersionUID = -339516038496531943L;
    private String sno;
    private String name;
    private String sex;
    public String getSno() {
        return sno;
    }
    public void setSno(String sno) {
        this.sno = sno;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{"  
                "sno='"   sno   '''  
                ", name='"   name   '''  
                ", sex='"   sex   '''  
                '}';
    }
}


public class Student implements Serializable{
    
    private static final long serialVersionUID = -339516038496531943L;
    private String sno;
    private String name;
    private String sex;
    public String getSno() {
        return sno;
    }
    public void setSno(String sno) {
        this.sno = sno;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{"  
                "sno='"   sno   '''  
                ", name='"   name   '''  
                ", sex='"   sex   '''  
                '}';
    }
}



@RestController
public class TestController {

    /**
     * http://localhost:8080/addstudent3
     * 入参:{"sno":"11124","name":"xiaoming","sex":"man"}
     * 返回:
     * Student{sno='11124', name='AOP-NAME', sex='man'}
     *
     * @param student
     * @param request
     * @return
     */
    @RequestMapping(value = "/addstudent3", method = RequestMethod.POST)
    public String saveStudent3(@RequestBody Student student, HttpServletRequest request) {
        return student.toString();
    }

    
        /**
     * http://localhost:8080/addstudent5
     * 参数:{"sno":"11124","name":"xiaoming","sex":"man"}
     * 返回:
     * Student{sno='11124', name='AOP-NAME-2', sex='man'}
     *
     * @param classRoom
     * @return
     */
    @RequestMapping(value = "/addstudent5", method = RequestMethod.POST)
    public String saveStudent5(@RequestBody ClassRoom classRoom) {
        return classRoom.toString();
    }
    
}

0 人点赞