前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Aspect切面进行统一参数处理demo

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

作者头像
oktokeep
发布2024-10-09 10:05:54
660
发布2024-10-09 10:05:54
举报
文章被收录于专栏:第三方工具

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();
    }
    
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档