這篇文章將為大家詳細(xì)講解有關(guān)springboot2.0中怎么通過(guò)自定義注解獲取方法返回值,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
為拜城等地區(qū)用戶(hù)提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及拜城網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站建設(shè)、拜城網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶(hù)提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶(hù)的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
springboot2.0 自定義注解通過(guò)類(lèi)或者方法切面并且獲取方法的返回值
新增一個(gè)自定義注解
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 { }
新增一個(gè)切面,
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 @Component public class DemoAspect { @Pointcut("@within(com.example.demo.annotation.TestA)")//注解在類(lèi)上的用 //@Pointcut("@annotation(com.example.demo.annotation.TestA)")//注解在方法上 public void addAdvice(){} @AfterReturning(returning = "rvl",pointcut="@within(com.example.demo.annotation.TestA)" )//注解在類(lèi)上面 //@AfterReturning(returning = "rvl",pointcut="@within(com.example.demo.annotation.TestA)" )//注解在方法上 public void afterReturn(JoinPoint joinPoint,Object rvl) throws ClassNotFoundException { Object[] args = joinPoint.getArgs();//參數(shù) log.info("--------args:{}",args.length); log.info("============打印日志開(kāi)始============"); log.info("target:{}",joinPoint.getTarget().getClass()); for(Object o:args) { log.info("參數(shù):{}",o); } log.info("返回參數(shù):{}",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(); //獲取方法名稱(chēng) log.info("============打印日志結(jié)束============"); } }
新增一個(gè)service類(lèi)
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 @TestA public 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 ms 2019-10-09 11:41:48.856 INFO 2264 --- [nio-8080-exec-1] com.example.demo.test.DemoTest : say is start 2019-10-09 11:41:48.858 INFO 2264 --- [nio-8080-exec-1] com.example.demo.Aspect.DemoAspect : --------args:2 2019-10-09 11:41:48.859 INFO 2264 --- [nio-8080-exec-1] com.example.demo.Aspect.DemoAspect : ============打印日志開(kāi)始============ 2019-10-09 11:41:48.859 INFO 2264 --- [nio-8080-exec-1] com.example.demo.Aspect.DemoAspect : target:class com.example.demo.test.DemoTest 2019-10-09 11:41:48.859 INFO 2264 --- [nio-8080-exec-1] com.example.demo.Aspect.DemoAspect : 參數(shù):say hello 2019-10-09 11:41:48.859 INFO 2264 --- [nio-8080-exec-1] com.example.demo.Aspect.DemoAspect : 參數(shù):origin 2019-10-09 11:41:48.860 INFO 2264 --- [nio-8080-exec-1] com.example.demo.Aspect.DemoAspect : 返回參數(shù):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-execution 2019-10-09 11:41:48.861 INFO 2264 --- [nio-8080-exec-1] com.example.demo.Aspect.DemoAspect : ============打印日志結(jié)束============
關(guān)于springboot2.0中怎么通過(guò)自定義注解獲取方法返回值就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
新聞標(biāo)題:springboot2.0中怎么通過(guò)自定義注解獲取方法返回值
標(biāo)題URL:http://aaarwkj.com/article36/goocpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站改版、商城網(wǎng)站、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站策劃、網(wǎng)站營(yíng)銷(xiāo)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)