Transmit Value by Customized Annotation in AOP Interception
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,1 Mydoing.javapackage sh.fudan.cxiao.MyAnno.annotation;import java.lang.annotation.ElementType;impor
千家信息网最后更新 2025年12月03日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;@Componentpublic 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@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; 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;@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
i.e.
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
c段ip地址是同一个服务器吗
2019北京网络安全训练营
apache数据库怎么打开
游戏软件开发的经营范围
数据库的功能设计指的是
尉氏县网络安全活动
靠谱的贸易管理软件开发流程
app系统软件开发哪家收费合理
1g网络技术百度百科
总工会网络安全工作小结
东莞市百纳网络技术有限公司
简述满足层次数据库的条件
服务器组装招聘
数据库表中如何设置唯一性
网络安全知识幼儿园亲子照
北大青鸟java软件开发
平台服务器
美团软件开发团队有多少人
欧米伽网络技术有限公司
网络安全厂家排名2018
无法从数据库拿数据
有什么警校文科能报网络安全
云科技网络技术团队
徐州软件开发公司排名
事实数据库有
双非院校网络安全博士点
国内服务器芯片厂商
简单谈谈怎样提高计算机网络安全
两个数据库的两张表怎么连接查询
服务器双显卡显示pci资源不足