目录结构:

一:自定义注解

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. angular入门--列表排序

    首先,先上代码 <html ng-app="app1"> <head> <meta charset='utf-8' /> <meta na ...

  2. Pop!_OS下安装C++编程工具

    Pop!_OS下C++编程 #0x0 Visual Studio Code #0x1 C++ 0x11 code::blocks #0x0 Visual Studio Code 下载安装vscode ...

  3. Pop!_OS安装与配置(二):基础配置

    Pop!_OS相关使用配置 #0x0 书接上回 #0x1 换源 #0x10 nopasswd sudo #0x11 换国内源 0x2 安装应用 #0x0 书接上回 首先请看上一章末尾, Pop!_OS ...

  4. 一道题理解setTimeout,Promise,async/await以及宏任务与微任务

    今天看到这样一道面试题: //请写出输出内容 async function async1() { console.log('async1 start'); await async2(); consol ...

  5. day02 java学习

    Java三大版本 Write once,Run anywhere. (基于JVM虚拟机) JavaSE 标准版 JavaME 嵌入式开发 JavaEE 企业级开发 Java三大名词 JDK Java开 ...

  6. bugku extract 变量覆盖

  7. MCU 51-2 LED and Digital tube Test

    点亮LED实验: #include <reg52.h> sbit LED1 = P1^; sbit LED2 = P1^; sbit LED8 = P1^; void main() { L ...

  8. SQL字符串拼接FOR XML PATH

    在工作中难免会遇到数据库中数据要进行拼接的问题,字符串拼接可以是用SQL的拼接也可以使用C#的拼接,本次说的是使用SQL进行拼接. 首先插入测试语句: --测试语句,准备创建表的语句:如下 CREAT ...

  9. 有效提高java编程安全性的12条黄金法则

    安全性是软件开发中最复杂,最广泛和最重要的考量之一.Java是具有许多内置安全性功能的开发平台,java在长期的发展过程中,已经经过了很多高强度的安全测试,并经常更新安全漏洞.并且Java生态系统还包 ...

  10. 原来不只是fastjson,这个你每天都在用的类库也被爆过反序列化漏洞!

    GitHub 15.8k Star 的Java工程师成神之路,不来了解一下吗! GitHub 15.8k Star 的Java工程师成神之路,真的不来了解一下吗! GitHub 15.8k Star ...