springboot2.0中怎么通过自定义注解获取方法返回值
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章将为大家详细讲解有关springboot2.0中怎么通过自定义注解获取方法返回值,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。springbo
千家信息网最后更新 2026年11月01日springboot2.0中怎么通过自定义注解获取方法返回值
这篇文章将为大家详细讲解有关springboot2.0中怎么通过自定义注解获取方法返回值,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
springboot2.0 自定义注解通过类或者方法切面并且获取方法的返回值
新增一个自定义注解
package com.example.demo.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Target;@Target({ElementType.METHOD,ElementType.FIELD,ElementType.TYPE})public @interface TestA {}新增一个切面,
package com.example.demo.Aspect;import lombok.extern.slf4j.Slf4j;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletRequest;/** * @auth: xinhui * @date: **/@Slf4j@Aspect@Componentpublic class DemoAspect { @Pointcut("@within(com.example.demo.annotation.TestA)")//注解在类上的用 //@Pointcut("@annotation(com.example.demo.annotation.TestA)")//注解在方法上 public void addAdvice(){} @AfterReturning(returning = "rvl",pointcut="@within(com.example.demo.annotation.TestA)" )//注解在类上面//@AfterReturning(returning = "rvl",pointcut="@within(com.example.demo.annotation.TestA)" )//注解在方法上 public void afterReturn(JoinPoint joinPoint,Object rvl) throws ClassNotFoundException { Object[] args = joinPoint.getArgs();//参数 log.info("--------args:{}",args.length); log.info("============打印日志开始============"); log.info("target:{}",joinPoint.getTarget().getClass()); for(Object o:args) { log.info("参数:{}",o); } log.info("返回参数:{}",rvl); log.info("kind:{}",joinPoint.getKind()); String classType = joinPoint.getTarget().getClass().getName(); Class> clazz = Class.forName(classType); String clazzName = clazz.getName(); String methodName = joinPoint.getSignature().getName(); //获取方法名称 log.info("============打印日志结束============"); }}新增一个service类
package com.example.demo.test;import com.example.demo.annotation.TestA;import com.example.demo.model.SayEntity;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Service;/** * @auth: xinhui * @date: 2019/10/8 8:52 下午 **/@Service("demoTest")@Slf4j@TestApublic class DemoTest {// @TestA public SayEntity say(String saystr,String origin) {log.info("say is start"); SayEntity say = new SayEntity(saystr, origin); return say; }}新增controller信息
package com.example.demo;import com.example.demo.test.DemoTest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.EnableAspectJAutoProxy;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@SpringBootApplication@RestController@EnableAspectJAutoProxy@ComponentScan(basePackages = {"com.example.demo"})public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Autowired DemoTest demoTest; @RequestMapping(value = "/demo/aop",method = RequestMethod.GET) public Object mapping(){ String say="say hello";String origin="origin"; return demoTest.say(say,origin); }}打印日志的信息
2019-10-09 11:41:42.829 INFO 2264 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''2019-10-09 11:41:42.832 INFO 2264 --- [ restartedMain] com.example.demo.DemoApplication : Started DemoApplication in 2.331 seconds (JVM running for 3.133)2019-10-09 11:41:48.810 INFO 2264 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'2019-10-09 11:41:48.810 INFO 2264 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'2019-10-09 11:41:48.819 INFO 2264 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 9 ms2019-10-09 11:41:48.856 INFO 2264 --- [nio-8080-exec-1] com.example.demo.test.DemoTest : say is start2019-10-09 11:41:48.858 INFO 2264 --- [nio-8080-exec-1] com.example.demo.Aspect.DemoAspect : --------args:22019-10-09 11:41:48.859 INFO 2264 --- [nio-8080-exec-1] com.example.demo.Aspect.DemoAspect : ============打印日志开始============2019-10-09 11:41:48.859 INFO 2264 --- [nio-8080-exec-1] com.example.demo.Aspect.DemoAspect : target:class com.example.demo.test.DemoTest2019-10-09 11:41:48.859 INFO 2264 --- [nio-8080-exec-1] com.example.demo.Aspect.DemoAspect : 参数:say hello2019-10-09 11:41:48.859 INFO 2264 --- [nio-8080-exec-1] com.example.demo.Aspect.DemoAspect : 参数:origin2019-10-09 11:41:48.860 INFO 2264 --- [nio-8080-exec-1] com.example.demo.Aspect.DemoAspect : 返回参数:SayEntity(say=say hello, eat=origin)2019-10-09 11:41:48.860 INFO 2264 --- [nio-8080-exec-1] com.example.demo.Aspect.DemoAspect : kind:method-execution2019-10-09 11:41:48.861 INFO 2264 --- [nio-8080-exec-1] com.example.demo.Aspect.DemoAspect : ============打印日志结束============
关于springboot2.0中怎么通过自定义注解获取方法返回值就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
注解
方法
参数
日志
信息
内容
切面
文章
更多
知识
篇文章
不错
名称
质量
o.a.c.c.C.
参考
帮助
有关
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
百单网互联网科技有限公司怎么样
西安纸贵互联网科技有限公司
苏州找大状互联网科技有限公司
互联网与新科技
万易互联网科技有限公司
丰台科技园 互联网
万达互联网科技
互联网前沿科技
华脉科技工业互联网产品
新华互联网科技2019
互联网科技博主是什么意思
温州喔噻互联网科技有限公司
呼和浩特新华互联网科技学院
保险科技服务互联网营销体系
上海钢联属于互联网科技股吗
北京市互联网新科技
互联网科技品牌
优雇互联网科技有限公司
恒生科技互联网指数十大成分股
前海云享互联网科技
互联网科技官网
互联网巨头科技公司
互联网 科技
以房互联网科技
菲比寻常互联网科技
居家互联网科技有限公司
青海省互联网新科技
团飞科技与伯堂互联网服务平台
速维有品互联网信息科技(苏州)有限公司
亚光科技低轨卫星互联网