In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
1 Mydoing.javapackage sh.fudan.cxiao.MyAnno.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.METHOD) public @ interface Mydoing {String name ();}
2 MyService.javapackage sh.fudan.cxiao.MyAnno.business;import org.springframework.stereotype.Component;import sh.fudan.cxiao.MyAnno.annotation.Mydoing;@Componentpublic class MyService {@ Mydoing (name= "# n") public int doSomething (int n) {System.out.println ("Executing pointcut function, i.e. Business operation"); return 100 minutes;}}
3 SupportingComponent.javapackage sh.fudan.cxiao.MyAnno.component;import java.lang.reflect.Method;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.core.DefaultParameterNameDiscoverer;import org.springframework.expression.EvaluationContext;import org.springframework.expression.Expression;import org.springframework.expression.ExpressionParser;import org.springframework.expression.spel.standard.SpelExpressionParser;import org.springframework.expression.spel.support.StandardEvaluationContext Import org.springframework.stereotype.Component;import sh.fudan.cxiao.MyAnno.annotation.Mydoing;@Component@Aspectpublic class SupportingComponent {@ Around ("@ annotation (sh.fudan.cxiao.MyAnno.annotation.Mydoing)") public Object doExtraThingForMydoingAnno (ProceedingJoinPoint joinPoint) throws Throwable {System.out.print ("before pointcut execution, get details of the annotation item 'name' on pointcut"); String nameEL = ""; MethodSignature signature = (MethodSignature) joinPoint.getSignature () Method method = joinPoint.getTarget (). GetClass (). GetMethod (signature.getName (), signature.getMethod (). GetParameterTypes ()); Mydoing mydoing = method.getAnnotation (Mydoing.class); nameEL = mydoing.name (); / / i.e. # n System.out.println ("=" + nameEL); System.out.print (nameEL + "'s value comes from the parameter with the same name on pointcut") / 1. Create parser ExpressionParser parser = new SpelExpressionParser (); Expression expression = parser.parse_Expression (nameEL); / / 2. Set up evaluation context EvaluationContext context = new StandardEvaluationContext (); Object [] args = joinPoint.getArgs (); / / the running time arguments value on pointcut DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer (); String [] parameterNames = discoverer.getParameterNames (method) / / parameter names on pointcut / / put the parameter name on pointcut and relevant running argument value into context for (int I = 0; I < parameterNames.length; iTunes +) {context.setVariable (parameterNames [I], args [I]) } / / 3. Parsing (To get the value of placeholder in annotation on pointcut) / / get the value of the running time argument whose name is same as details of the annotation item 'name' on pointcut, i.e. # n Integer In = (Integer) expression.getValue (context); System.out.println (", value is" + In); int base= 1000; if (In%2==0) base=2000 If (In==0) / / Do not execute pointcut when the argument equals zero return-1; Integer result = (Integer) joinPoint.proceed (); / / execute the pointcut System.out.println ("after pointcut execution, subsequently process the returned value from pointcut."); result + = base + In; return result; / / the returned value should be compatible with the returned type from pointcut}}
4 MyAnnoApplication.javapackage sh.fudan.cxiao.MyAnno;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import sh.fudan.cxiao.MyAnno.business.MyService;@SpringBootApplicationpublic class MyAnnoApplication {public static void main (String [] args) {ApplicationContext ctx = new AnnotationConfigApplicationContext (MyAnnoApplication.class); MyService myService = ctx.getBean (MyService.class) System.out.println ("The result is" + myService.doSomething (5)); System.out.println ("The result is" + myService.doSomething (6)); System.out.println ("The result is" + myService.doSomething (0));}}
5 the snapshot of running
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.