您当前的位置: 首页 >  spring

梁云亮

暂无认证

  • 1浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Spring 中自定义注解

梁云亮 发布时间:2022-05-27 10:31:59 ,浏览量:1

定义注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnno {
   String param();//注解传递的参数,可不加
}
定义切面
@Component
@Aspect
public class MyInterceptor {

    @Before(value = "execution(public * com.hc.controller.*Controller.*()) && @annotation(myAnno)")
    public void proceed(MyAnno myAnno) throws Throwable {
        System.out.println("***********************");
        System.out.println(myAnno.param());
    }

}
定义Controller

在com.hc.controller包下面定义控制器:

@RestController
public class FunController {

    @RequestMapping("/fun1")
    public void fun1( ) {
        System.out.println("fun1");
    }

    @MyAnno(param = "ath")
    @RequestMapping("/fun2")
    public void fun2( ) {
        System.out.println("fun2");
    }
}
关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.0411s