Transmit Value by Customized Annotation in AOP Interception
1 Mydoing.java
package 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.java
package sh.fudan.cxiao.MyAnno.business; import org.springframework.stereotype.Component; import sh.fudan.cxiao.MyAnno.annotation.Mydoing; @Component public class MyService { @Mydoing (name="#n") public int doSomething(int n) { System.out.println("Executing pointcut function, i.e. business operation"); return 100*n; } }
3 SupportingComponent.java
package 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 @Aspect public 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.parseExpression(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; i++) { 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.java
package 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; @SpringBootApplication public 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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/32498/viewspace-2642313/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Spring中通過Annotation來實現AOPSpring
- Annotation
- Java建立AnnotationJava
- Annotation註解
- Transmit教程|如何將Transmit傳輸到新的Mac並匯入資料MITMac
- Transmit 5 for MacFTP客戶端MITMacFTP客戶端
- Java —— 註解(Annotation)Java
- @EnableAsync annotation metadata was not injected
- MacFTP客戶端——Transmit 5 for MacMacFTP客戶端MIT
- Transmit 5 for Mac(FTP客戶端)MITMacFTP客戶端
- java-Annotation註解Java
- Java之註解(Annotation)Java
- Spring常用Annotation簡介Spring
- PathVariable annotation was empty on param 0
- ch11_enum_and_annotation
- Java註解(Annotation)詳解Java
- Spring Boot Configuration Annotation Processor not configuredSpring Boot
- annotation之context:annotation-config與 context:component-scan的區別與聯絡Context
- Transmit 5中文最新資源5.10.4MIT
- Transmit:mac檔案傳輸的高效工具MITMac
- Android APT(Annotation Processing Tool) 實踐AndroidAPT
- Android 註解系列之Annotation(二)Android
- ANNOVAR region-based annotation-上篇
- <mvc:annotation-driven/>使用介紹MVC
- Java進階(一)Annotation(註解)Java
- golang multiple-value xxx in single-value contextGolangContext
- Transmit for mac(FTP/SFTP客戶端)中文版MITMacFTP客戶端
- FTP/SFTP客戶端:Transmit Mac中文版FTP客戶端MITMac
- [20231103]sqlplus column new_value old_value.txtSQL
- [20230303]sqlplus column new_value old_value.txtSQL
- Java 註解 (Annotation)淺入深出Java
- 使用Django annotation,提升django查詢效能Django
- Java註解(Annotation):請不要小看我!Java
- AOP
- JavaScript select valueJavaScript
- @ConfigurationProperties和@Value
- Oracle分析函式-first_value()和last_value()Oracle函式AST
- AOP詳解之三-建立AOP代理後記,建立AOP代理