Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get the return value of the method through custom annotation in springboot2.0

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to get the returned value through custom annotations in springboot2.0. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Springboot2.0 custom annotations cut through the class or method and get the return value of the method

Add a custom comment

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 {}

Add a new section

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)") / / Note on the class with / / @ Pointcut ("@ annotation (com.example.demo.annotation.TestA)") / / Note on the method public void addAdvice () {} @ AfterReturning (returning = "rvl") Pointcut= "@ within (com.example.demo.annotation.TestA)") / / comment on the class / / @ AfterReturning (returning = "rvl", pointcut= "@ within (com.example.demo.annotation.TestA)") / / comment on the method public void afterReturn (JoinPoint joinPoint,Object rvl) throws ClassNotFoundException {Object [] args = joinPoint.getArgs () / / Parameter log.info ("- args: {}", args.length); log.info ("= print log start ="); log.info ("target: {}", joinPoint.getTarget (). GetClass ()); for (Object o:args) {log.info ("Parameter: {}", o) } log.info ("return parameters: {}", 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 () / / get the method name log.info ("= end of print log =");}}

Add a new service class

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: 8:52 on 2019-10-8 * * / @ Service ("demoTest") @ Slf4j@TestApublic class DemoTest {/ / @ TestApublic SayEntity say (String saystr,String origin) {log.info ("say is start") SayEntity say = new SayEntity (saystr, origin); return say;}}

Add controller information

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);}}

Print information for the log

2019-10-09 11 o.s.b.w.embedded.tomcat.TomcatWebServer 41o.s.b.w.embedded.tomcat.TomcatWebServer: Tomcat started on port (s): 8080 (http) with context path '2019-10-09 11 o.s.b.w.embedded.tomcat.TomcatWebServer 42.832 INFO 2264-- [restartedMain] com.example.demo.DemoApplication: Started DemoApplication in 2.331 seconds (JVM running for 3.133) 2019-10-09 111L INFO 2264 [nio-8080-exec-1] o.a.c.c. [tomcat] .[ localhost]. [/]: Initializing Spring DispatcherServlet 'dispatcherServlet'2019-10-09 11 INFO 41dispatcherServlet'2019 48.810 INFO 2264-[nio-8080-exec-1] o.s.web.servlet.DispatcherServlet: Initializing Servlet' dispatcherServlet'2019-10-09 11Fran 41hand48.819 INFO 2264-[nio-8080-exec-1] o.s.web .servlet.DispatcherServlet: Completed initialization in 9 ms2019-10-09 11 INFO 41purl 48.856 INFO 2264-[nio-8080-exec-1] com.example.demo.test.DemoTest: say is start2019-10-09 111purve48.858 INFO 2264-[nio-8080-exec-1] com.example.demo.Aspect.DemoAspect:-args:22019-10-09 111Fringe 48.859 INFO 2264-[nio-8080-exec-1] com.example.demo.Aspect.DemoAspect: = start printing the log = 2019-10-09 11 11 INFO 48.859 INFO 2264-[nio-8080-exec-1] com.example.demo.Aspect.DemoAspect: target:class com.example.demo.test.DemoTest2019-10-09 11 11) 41lo 48.859 INFO 2264-[nio-8080-exec-1] com.example.demo.Aspect .DemoAspect: parameter: say hello2019-10-09 11 nio-8080-exec-1 41 nio-8080-exec-1 INFO 2264-[DemoAspect] com.example.demo.Aspect.DemoAspect: parameter: origin2019-10-09 11 41 purse 48.860 INFO 2264-[nio-8080-exec-1] com.example.demo.Aspect.DemoAspect: return parameter: SayEntity (say=say hello) Eat=origin) 2019-10-09 11 nio-8080-exec-1 41 nio-8080-exec-1 INFO 2264-[nio-8080-exec-1] com.example.demo.Aspect.DemoAspect: kind:method-execution2019-10-09 11 11 V 41V 48.861 INFO 2264-[nio-8080-exec-1] com.example.demo.Aspect.DemoAspect: = end of print log = about how to get the returned value from the custom annotation method in springboot2.0 and that's it. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 231

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report