【Spring】 (1)Aspect 自定义注解 使用该注解的地方 自动注入该注解里传的参数
来源:互联网 发布:全国网络诈骗报警中心 编辑:程序博客网 时间:2024/06/10 04:31
package com.example.demo_1_3;import java.lang.annotation.*;/** * Created by WangBin on 2017/4/6. * <p> * 编写拦截规则的注解 */@Target(ElementType.METHOD)//方法@Retention(RetentionPolicy.RUNTIME)// 注解会在class字节码文件中存在,在运行时可以通过反射获取到@Documented//@Document:说明该注解将被包含在javadoc中public @interface Action { String name(); String age();}//java中元注解有四个:@Retention @Target @Document @Inherited;//// @Retention:注解的保留位置 //// @Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含// @Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,// @Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到// @Target:注解的作用目标// @Target(ElementType.TYPE) //接口、类、枚举、注解// @Target(ElementType.FIELD) //字段、枚举的常量// @Target(ElementType.METHOD) //方法// @Target(ElementType.PARAMETER) //方法参数// @Target(ElementType.CONSTRUCTOR) //构造函数// @Target(ElementType.LOCAL_VARIABLE)//局部变量// @Target(ElementType.ANNOTATION_TYPE)//注解// @Target(ElementType.PACKAGE) ///包////@Document:说明该注解将被包含在javadoc中//// @Inherited:说明子类可以继承父类中的该注解
package com.example.demo_1_3;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;/** * Created by WangBin on 2017/4/6. * * */@Configuration@ComponentScan("com.example.demo_1_3_3.aop")@EnableAspectJAutoProxy//注解开启 spring 对 AspectJ的支持public class AopConfig {}
package com.example.demo_1_3;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * 目前理解为 接口和 实现类的方式调用方法 */public class DemoApplication {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AopConfig.class);DemoService demoService = applicationContext.getBean(DemoService.class);demoService.add();demoService.getInfo();}}
package com.example.demo_1_3;import org.springframework.stereotype.Service;/** * Created by WangBin on 2017/4/6. * *编写使用注解的被拦截类 */@Service//注解为beenpublic class DemoService { @Action(name="jack",age="21") public void add(){} @Action(name="jack1",age="22") public void getInfo(){ System.err.println("---------------------"); }}
package com.example.demo_1_3;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.stereotype.Component;import java.lang.reflect.Method;/** * Created by WangBin on 2017/4/6. * *编写切面 */@Aspect//通过@Aspect注解声明一个切面@Component//通过@Component 让此切面成为 spring 管理的beenpublic class LogAspect { @Pointcut("@annotation(com.example.demo_1_3.Action)")//通过@Pointcut 注解 声明切点 public void annotationPointCut(){} @After("annotationPointCut()")//通过After 注解声明一个建言 并使用Pointcut定义 // 的切点 public void after(JoinPoint joinPoint){ MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Action action = method.getAnnotation(Action.class); System.err.println("姓名:"+action.name());//通过反射获得注解上的属性 然后做日志记录相关操作 System.err.println("年龄:"+action.age());//通过反射获得注解上的属性 然后做日志记录相关操作 }}
阅读全文
0 0
- 【Spring】 (1)Aspect 自定义注解 使用该注解的地方 自动注入该注解里传的参数
- Spring 中使用@Aspect 控制自定义注解
- spring的注解注入
- Spring的注入,注解
- 方便的自动Spring注解注入
- Spring自定义注解的使用
- Spring中Aspect注解使用
- 注解该如何使用
- spring Aspect 实现自定义注解的日志记录,有时候注解类不起作用的原因分析
- Spring 基于 Aspect 注解的增强实现
- Spring的自定义注解
- Aspect编程中@Around注解的使用
- spring aspect 注解
- spring的注解注入方式
- spring注解注入的配置
- spring依赖注入的注解
- 自定义注解的使用
- @Transactional事务注解指定该注解采用的事务管理器
- leetcode 209. Minimum Size Subarray Sum 3. Longest Substring Without Repeating Characters
- AndroidStudio插件管理
- 大数据任务安排第一期
- 关于数据库设计的一点心得
- TabLayout 关联了 ViewPager 下划线不见了?
- 【Spring】 (1)Aspect 自定义注解 使用该注解的地方 自动注入该注解里传的参数
- NOIP 2015 Day2 T3 运输计划(二分+dfs序+树上差分+倍增LCA)
- 开发相关的测试
- 生成mnist_model_graph.pb
- BZOJ1131 [POI2008]Sta
- 考试总结6
- 神经网络中的「注意力」是什么?怎么用?
- 不用恐惧AI的高速发展,论击败阿法狗(零)最简单的方法
- 微软 CEO 纳德拉访谈:人工智能的大方向与未来是什么?