当前位置: 首页 >服务端 > Spring-Boot:拦截器注解范例

Spring-Boot:拦截器注解范例

package com.example.aop;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.Target;import static java.lang.annotation.RetentionPolicy.RUNTIME;/** * Created by liz19 on 2017/1/30. */@Target(ElementType.METHOD)@Retention(RUNTIME)@Documentedpublic @interface Action {String name();}
package com.example.aop;import org.springframework.stereotype.Service;/** * Created by liz19 on 2017/1/30. */@Servicepublic class DemoAnnotationService {@Action(name="注解式拦截的add操作")public void add(){System.out.println("clas DemoAnnotationService");}}
package com.example.aop;import org.springframework.stereotype.Service;/** * Created by liz19 on 2017/1/30. */@Servicepublic class DemoMethodService {public  void add(){System.out.println("class DemoMethodService");}}
package com.example.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;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.stereotype.Component;import java.lang.reflect.Method;/** * Created by liz19 on 2017/1/30. */@Aspect@Componentpublic class LogAspect {@Pointcut("@annotation(com.example.aop.Action)")public void annotationPointCut(){}@After("annotationPointCut()")public void after(JoinPoint joinPoint){MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();Action action = method.getAnnotation(Action.class);System.out.println("注解式拦截--------Name: "+action.name());}@Before("execution(* com.example.aop.DemoMethodService.*(..))")public void before(JoinPoint joinPoint){MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();System.out.println("方法规则拦截拦截---------"+ method.getName());}}
package com.example;import com.example.aop.AopConfig;import com.example.aop.DemoAnnotationService;import com.example.aop.DemoMethodService;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Created by liz19 on 2017/1/30. */public class AopApp {public static void main(String[] args){AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);demoAnnotationService.add();demoMethodService.add();context.close();}}
package com.example.aop;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;/** * Created by liz19 on 2017/1/30. */@Configuration@ComponentScan("com.example.aop")@EnableAspectJAutoProxypublic class AopConfig {}

 

1.通过@Aspect注解声明一个切面

2.通过@Component让此切面成为Spring容器管理的Bean

3.通过@PointCut注解声明切点

4.通过@After注解声明一个建言,并用@PointCut定义的切点

5.通过发射获得注解上的属性,然后做日志记录的相关操作,下面的相同

6.通过@Before注解声明一个建言,此建言直接使用拦截规则作为参数

7.通过@EnableAspectJAutoProxy开启对AspectJ支持

 

作者:MrMrCash
来源链接:https://www.cnblogs.com/MarchThree/p/6358576.html

版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaclub.cn/server/112190.html

标签:Spring注解
分享给朋友: