目录结构:

一:自定义注解

package org.example.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//表示该注解只可以在方法上使用。
@Target(ElementType.METHOD)
//表示该注解一直存活到被加载进JVM。
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String message() default ""; int code() default 0;
}

@Target:

//作用于类、接口和枚举上
ElementType.TYPE
//作用于字段、枚举常量
ElementType.FIELD
//作用于方法上
ElementType.METHOD
//作用于方法的参数上
ElementType.PARAMETER
//作用于构造函数上
ElementType.CONSTRUCTOR
//作用于局部变量上
ElementType.LOCAL_VARIABLE
//作用于注解上
ElementType.ANNOTATION_TYPE
//作用于包上
ElementType.PACKAGE

@Retention:

用于声明注解的生命周期。
//注解仅仅保存于源码中,在被编译成class文件时就失效
RetentionPolicy.SOURCE
//默认策略,在编译成class文件后仍有效,在被装载进JVM时就失效
RetentionPolicy.CLASS
//在JVM中仍存在,可以通过反射获取到注解的属性值
RetentionPolicy.RUNTIME

@Inherited:表示该注解可以被继承。

@Document:表示该注解会被加载进Javadoc中。

二:DemoController

@RestController
@RequestMapping("demo")
public class DemoController {
@GetMapping
@MyAnnotation(message = "songkw", code = 23)
public Object demo() {
return "annotation test";
}
}

三:AOP

@Aspect
@Component
public class DemoAOP { @Before(value = "execution(public * org.example.controller.*.*(..))")
   //@Before(value = "@annotation(org.example.annotation.MyAnnotation)")
public void before(JoinPoint joinPoint) throws NoSuchMethodException {
Object target = joinPoint.getTarget();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = target.getClass().getMethod(signature.getName(), signature.getParameterTypes());
MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
int code = annotation.code();
String message = annotation.message();
System.err.println("code:" + code);
System.err.println("message:" + message);
}
}

四:启动类

@SpringBootApplication(scanBasePackages = {"org.example.*"})
@EnableAspectJAutoProxy
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}

五:pom.xml

  <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>

使用AOP获取自定义注解的内容的更多相关文章

  1. AOP中获取自定义注解的参数值

      目录 一.利用注解实现AOP的基本流程 1.1.创建一个注解,用来注解切点(pointcut) 1.2.创建一个service,使用上面定义的注解来指定切点 1.3.创建Aspect,增加业务逻辑 ...

  2. AOP获取方法注解实现动态切换数据源

    AOP获取方法注解实现动态切换数据源(以下方式尚未经过测试,仅提供思路) ------ 自定义一个用于切换数据源的注解: package com.xxx.annotation; import org. ...

  3. java反射机制获取自定义注解值和方法

    由于工作需求要应用到java反射机制,就做了一下功能demo想到这些就做了一下记录 这个demo目的是实现动态获取到定时器的方法好注解名称,废话不多说了直接上源码 1.首先需要自定义注解类 /** * ...

  4. AOP通过反射获取自定义注解

    自定义注解: @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component publ ...

  5. 用AOP拦截自定义注解并获取注解属性与上下文参数(基于Springboot框架)

    目录 自定义注解 定义切面 获取上下文信息JoinPoint ProceedingJoinPoint 定义测试方法 测试结果 小结 AOP可以用于日志的设计,这样话就少不了要获取上下文的信息,博主在设 ...

  6. spring AOP 和自定义注解进行身份验证

    一个SSH的项目(springmvc+hibernate),需要提供接口给app使用.首先考虑的就是权限问题,app要遵循极简模式,部分内容无需验证,用过滤器不能解决某些无需验证的方法 所以最终选择用 ...

  7. AOP 实现自定义注解

    1.自定义注解2.编写 AOP3.测试 1.自定义注解 package com.base.yun.spring.aop; import java.lang.annotation.Documented; ...

  8. 利用Spring AOP和自定义注解实现日志功能

    Spring AOP的主要功能相信大家都知道,日志记录.权限校验等等. 用法就是定义一个切入点(Pointcut),定义一个通知(Advice),然后设置通知在该切入点上执行的方式(前置.后置.环绕等 ...

  9. 基于SpringBoot 、AOP与自定义注解转义字典值

    一直以来,前端展示字典一般以中文展示为主,若在表中存字典值中文,当字典表更改字典值对应的中文,会造成数据不一致,为此设置冗余字段并非最优方案,若由前端自己写死转义,不够灵活,若在业务代码转义,臃肿也不 ...

随机推荐

  1. 理解css中min-width和max-width,width与它们之间的区别联系

    css中,min-width是用来限制元素的最小宽度,max-width用来限制元素的最大宽度,也就是说当元素的width大于max-width,或者小于min-width.就被它们的值所代替,尤其适 ...

  2. 解决Chrome插件安装时程序包无效:"CRX_HEADER_INVALID"

    打开chorme的扩展程序(设置——>更多工具——>扩展程序)chrome://extensions 选择开发者模式 拖拽.crx至Chrome的扩展程序列表 安装失败 报错为:程序包无效 ...

  3. MySQL入门(引擎、数据类型、约束)

    MySQL入门(二) 表的引擎:驱动数据的方式 - 数据库优化 # 概要:引擎是建表规定的,提供给表使用,不是数据库的 # 展示所有引擎 show engines; # innodb(默认): 支持事 ...

  4. 数据可视化实例(十二): 发散型条形图 (matplotlib,pandas)

    https://datawhalechina.github.io/pms50/#/chapter10/chapter10 如果您想根据单个指标查看项目的变化情况,并可视化此差异的顺序和数量,那么散型条 ...

  5. Kafka Eagle V2.0.0新版预览

    1.概述 Kafka Eagle是一款用于管理Kafka的监控系统,且完全开源.当前Kafka Eagle发布了2.0.0版本.今天笔者就为大家来介绍一下2.0.0更新了哪些功能. 官网地址:http ...

  6. Shaderlab-10chapter-立方体纹理、玻璃效果

    10.1.1天空盒子 window - Lighting - skyMaterial 创建mat,shader选自带的6 side shader 确保相机选skybox 如果某个相机需要覆盖,添加sk ...

  7. Thymeleaf模板引擎学习

    开发传统Java WEB项目时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用JSP页面进行页面渲染了.从而Thymeleaf提供了一个用于整合Spring MVC的可选模 ...

  8. C#中String与byte[]的相互转换

    从文件中读取字符串 string filePath = @"C:\Temp.xml"; string xmlString= File.ReadAllText(filePath); ...

  9. bzoj3621我想那还真是令人高兴啊

    bzoj3621我想那还真是令人高兴啊 题意: T组数据,每组给出两个三角形各点坐标,要求求出一个点使第一个三角形可以绕这个点放缩和旋转得到另一个三角形.T≤10,坐标为≤10000的实数,数据保证三 ...

  10. threadLocal源码土话解说

    前言 废话不多说,先了解什么是threadLocal,下面是threadLocal类的说明注释, 这段话大致(猜的)意思是,改类为线程提供了一个局部变量,但是呢,这个变量和普通的变量又有所不同,怎么不 ...