首页 > 编程语言 >Aspect切面进行统一参数处理demo

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

时间:2022-10-11 16:11:53浏览次数:67  
标签:return name demo sex 切面 Aspect sno public String

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

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

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();
    }
    
}

 

标签:return,name,demo,sex,切面,Aspect,sno,public,String
From: https://www.cnblogs.com/oktokeep/p/16779577.html

相关文章

  • RequestBodyAdvice和注解方式进行统一参数处理demo
    RequestBodyAdvice和注解方式进行统一参数处理demo@Target({ElementType.METHOD,ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic@inte......
  • 第06篇: Spring AOP面向切面编程,最佳实践分享。
    公众号:西魏陶渊明<br/>天下代码一大抄,抄来抄去有提高,看你会抄不会抄!(文章目录)一、前言面向切面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象......
  • AspNetCore中 使用 Grpc 简单Demo
    为什么要用Grpc跨语言进行,调用服务,获取跨服务器调用等目前我的需要使用我的抓取端是go写的查询端用Net6写的导致很多时候我需要把一些临时数据写入到Redis在两......
  • 如何使用vuforiaSDK开发第一个AR demo应用
    1.IfyouareusingorplanningtousetheAndroidStudioIDE,herearesomenotesabouthowtoimportandbuildtheVuforiasamples:首先安装好环境:JDK,NDK,AndroidS......
  • Flink自定义Oracle的Source的Demo
    1.实体类@Data@Builder//创建对象@NoArgsConstructor//无参构造函数@AllArgsConstructor//有参构造函数publicclassOrderSink{privateintid;......
  • flutter系列之:按比例缩放的AspectRatio和FractionallySizedBox
    目录简介AspectRatioFractionallySizedBox总结简介我们在构建UI的时候,为了适应不同的屏幕大小,通常需要进行一些自适应的配置,而最常见的自适应就是根据某个宽度或者高度自......
  • FlinkSQL的DataStream和Table互转的Demo
    1.构建UserLog对象@Data@Builder//创建对象@NoArgsConstructor//无参构造函数@AllArgsConstructor//有参构造函数publicclassUserLog{privateStr......
  • ThreadLocal本地局部线程demo
    ThreadLocal本地局部线程demoimportorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importjava.util.HashMap;importjava.util.Map;/***本工具只能保存一......
  • Thread的join方法demo
    Thread的join方法demo/***关于join官方的解释是Waitsforthisthreadtodie.也就是等待一个线程结束。*/publicclassThreadJoinTest{publicstaticvo......
  • Demo25_方法的调用
    //方法调用加法典例packagecom.HuanXin.Fan_Fa_5;publicclassDemo01{//main方法publicstaticvoidmain(String[]args){intA=add(3,4);//调用a......