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

The method of obtaining method parameters and parameter values by spring through aop

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "the method of obtaining method parameters and parameter values by spring through aop". In daily operation, I believe that many people have doubts about the method parameters and parameter values obtained by spring through aop. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "spring obtains method parameters and parameter values through aop". Next, please follow the editor to study!

Catalogue

Spring acquires method parameters and parameter values through aop

Custom annotation

Cut plane

Aop section annotation and parameter acquisition

1. The definition requires a section of the note

2. Annotate the methods that need to be cut.

3. Define the section

Spring acquires method parameters and parameter values through aop custom annotations package com.xiaolc.aspect; import java.lang.annotation.*; / * * @ author lc * @ date 2019-9-10 * / @ Documented@Target ({ElementType.TYPE, ElementType.METHOD}) @ Retention (RetentionPolicy.RUNTIME) public @ interface LiCheng {} section package com.xiaolc.aspect; import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect Import org.springframework.core.DefaultParameterNameDiscoverer;import org.springframework.core.ParameterNameDiscoverer;import org.springframework.stereotype.Component; import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map; / * get the annotation value on the method * / @ Component@Aspectpublic class AuditAnnotationAspect {@ Around ("@ annotation (liCheng)") private static Map getFieldsName (ProceedingJoinPoint joinPoint,LiCheng liCheng) throws ClassNotFoundException, NoSuchMethodException {String classType = joinPoint.getTarget (). GetClass (). GetName () String methodName = joinPoint.getSignature (). GetName (); / / Parameter values Object [] args = joinPoint.getArgs (); Class [] classes = new Class [args.length]; for (int k = 0; k < args.length) IsPrimitive () {if (! args [k] .getClass (). GetClass ()) {/ / gets the encapsulation type instead of the base type String result = args [k] .getClass (). GetName (); Class s = map.get (result); classes [k] = s = = null? Args [k] .getClass (): s;}} ParameterNameDiscoverer pnd = new DefaultParameterNameDiscoverer (); / / gets the specified method. The second parameter may not be passed, but in order to prevent overloading, you still need to pass in the parameter type Method method = Class.forName (classType) .getMethod (methodName, classes); / / Parameter name String [] parameterNames = pnd.getParameterNames (method) / / encapsulate parameters and parameter values through map HashMap paramMap = new HashMap (); for (int I = 0; I < parameterNames.length; iTunes +) {paramMap.put (parameterNames [I], args [I]); System.out.println ("parameter name:" + parameterNames [I] + "\ nparameter value" + args [I]);} return paramMap } private static HashMap map = new HashMap () {{put ("java.lang.Integer", int.class); put ("java.lang.Double", double.class); put ("java.lang.Float", float.class); put ("java.lang.Long", Long.class); put ("java.lang.Short", short.class) Put ("java.lang.Boolean", boolean.class); put ("java.lang.Char", char.class);}};} aop section annotation and parameter acquisition

Aop is often used in our work. Here, aop uses the basic method to obtain the parameters and annotations used in the pointcut as an example.

1. Define the annotation @ Target (ElementType.METHOD) @ Retention (RetentionPolicy.RUNTIME) @ Documentedpublic @ interface AnnDemo {String value (); boolean isAop () default true;} 2. Annotate @ RestController@RequestMapping ("/ order") public class OrderController {@ Autowired private OrderService orderService in the method where the section is required. @ RequestMapping ("/ all") @ AnnDemo (value = "all", isAop = false) public List findAll () {List list = orderService.getOrderList (); return list;} @ RequestMapping ("/ page") @ AnnDemo (value = "page") public List findPage (@ RequestParam ("username") String username) {List listPage = orderService.getOrdersListPage (); return listPage;}} 3, define section

Get tangent point notes, methods, and parameters in the section

@ Aspect@Componentpublic class AspectDemo {@ Pointcut (value = "execution (* com.yin.freemakeradd.controller..* (..)") Public void excetionMethod () {} @ Pointcut (value = "execution (* com.yin.freemakeradd.controller..* (..) & & @ annotation (AnnDemo)") public void excetionNote () {} @ Before ("excetionMethod ()") public void testBefore (JoinPoint joinPoint) {System.out.println ("--pre-notification--") Object [] args = joinPoint.getArgs (); for (Object arg: args) {System.out.println (arg);}} @ Around (value = "execution (* com.yin.freemakeradd.controller..* (..) & & @ annotation (AnnDemo)") public Object testBeforeNote (ProceedingJoinPoint joinPoint) throws Throwable {/ / signature of the most frequently used notifications Signature signature = joinPoint.getSignature () MethodSignature msg= (MethodSignature) signature; Object target = joinPoint.getTarget (); / / method to get annotations Method method = target.getClass (). GetMethod (msg.getName (), msg.getParameterTypes ()); / / get annotations AnnDemo annotation = method.getAnnotation (AnnDemo.class) by method; Object proceed; / / get parameter Object [] args = joinPoint.getArgs () System.out.println (annotation.value ()); System.out.println (annotation.isAop ()); for (Object arg: args) {System.out.println (arg);} if (Objects.isNull (annotation) | |! annotation.isAop () {System.out.println ("No processing"); proceed = joinPoint.proceed () } else {System.out.println ("enter aop judgment"); proceed = joinPoint.proceed (); if (proceed instanceof List) {List proceedLst = (List) proceed; if (! CollectionUtils.isEmpty (proceedLst)) {TbOrder tbOrder = new TbOrder (); tbOrder.setPaymentType ("fffffffffffffffffff") ArrayList tbOrderLst = new ArrayList (); tbOrderLst.add (tbOrder); return tbOrderLst;}} System.out.println (proceed);} return proceed }} at this point, the study on "spring's method of obtaining method parameters and parameter values through aop" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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: 0

*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

Development

Wechat

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

12
Report