Tag: aop

带注释的类中带注释的方法或方法的切入点

我需要使用@X注释的类中的方法或使用@X注释的方法的切入点 。 我还需要注释对象 。 如果类和方法都被注释, 我更喜欢将方法注释作为参数 。 我尝试了以下操作,这会产生“不一致的绑定”警告。 (为什么不将它们设置为null?) @Around(“@annotation(methodLevelX) || @within(classLevelX)”) public Object advise(ProceedingJoinPoint pjp, X methodLevelX, X classLevelX) 以下内容创建了“参数(x)跨越’||’的模糊绑定 在切入点“警告。 (在我看来,这不一定有意义:为什么不绑定第一个短路评估?) @Around(“@annotation(x) || @within(x)”) public Object advise(ProceedingJoinPoint pjp, X x) 如果存在类和方法注释,则将先前的尝试拆分为两个自然会导致两个方法调用。 我知道我可以通过这样的切入点获得带有reflection的方法和类以及我想要的注释: @Around(“@annotation(com.package.X) || @within(com.package.X)”) 但我不愿意。 有没有“一个切入点,一个方法,一个注释参数”,我的要求的解决方案不需要反思?

使用Spring AOP记录好主意吗?

我现在正在阅读Spring,其中一个用于使用AOP的示例是记录方法调用的开始和结束。 我还读到使用AOP会影响性能。 对于这种类型的日志记录,使用Spring AOP是个好主意吗? 我的理解是Spring使用Dynamic AOP会更好地将静态AOP(如AspectJ)用于这种类型的AOP。 当然,我工作的公司的编码策略需要大量的日志记录,我希望减少我必须编写的日志代码的数量,并提高代码的可读性。 我吠叫错了树吗?

如何在Spring中拦截静态方法?

主题行基本上都说明了一切。 我有一个静态方法,我想拦截,以便可以应用它周围的建议。 我可以使用任何非静态方法,但我不确定如何允许拦截静态方法。

AspectJ切入点表达式匹配任何位置的参数注释

我正在尝试定义切入点表达式以匹配包含用特定注释注释的参数的方法,无论参数位于何处。在我的情况下,我正在寻找@Constraint注释。 例如: 匹配方法: public void method1(@Constraint Car car) public void method2(String id, @Constraint Plane plane) public void method3(Wheel wheel, @Constraint List trains, @Constraint Plane plane) public void method4(Motor motor, @Constraint Set trains, Bicycle bike, Wheel wheel) public void method5(Wing wing, Motorcycle moto, @Constraint Truck truck, Bicycle bike, Wheel wheel) 到目前为止,我已经尝试了以下表达式而没有运气: @Before(“execution(public * *.*(..)) and @args(com.example.Constraint)”) […]

具有注释参数的切入点匹配方法

在以下情况下,我需要使用与方法匹配的切入点创建方面: 它是用MyAnnotationForMethod注释的 其中一个参数(可以有很多)用@MyAnnotationForParam注释(但也可以有其他注释)。 方面类看起来像这样 @Pointcut(“execution(@MyAnnotationForMethod * *(..,@aspects.MyAnnotationForParam Object, ..)) && args(obj)”) void myPointcut(JoinPoint thisJoinPoint, Object obj) { } @Before(“myPointcut(thisJoinPoint , obj)”) public void doStuffOnParam(JoinPoint thisJoinPoint, Object obj) { LOGGER.info(“doStuffOnParam :”+obj); } 注释方法 @MyAnnotationForMethod public string theMethod(String a, @MyAnnotationForParam @OtherAnnotation Object obj, Object b){ LOGGER.info(a+obj+b); } 随着eclipse – >警告:关于poincut: Multiple markers at this line – no […]

Aspectj覆盖方法的参数

我正在开发一个方面来检查setter方法的参数并用空值覆盖空字符串。 到目前为止这是我的州: @Before(“execution(* de.foo.entity.*.set*(..)) && args(java.lang.String)”) public void check(final JoinPoint jp) { LOGGER.debug(jp.getSignature().toLongString()); Object[] args = jp.getArgs(); for (int i = 0; i < args.length; i++) { if (args[i] instanceof String && ((String) args[i]).isEmpty()) { args[i] = null; } } } 不幸的是,覆盖语句args[i] = null; 现在有效! 有谁知道我该怎么覆盖它? 干杯, 凯文

在非单例bean上修复Spring代理上的BeanNotOfRequiredTypeException?

我在从应用程序上下文中提取Spring bean时遇到问题。 当我尝试; InnerThread instance = (InnerThread) SpringContextFactory.getApplicationContext().getBean(“innerThread”, InnerThread.class); 我明白了 org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named ‘innerThread’ must be of type [com.generic.InnerThread], but was actually of type [$Proxy26] 如果没有getBean()调用中的指定类,我会得到一个ClassCastException(您可以在下面详细介绍)。 InnerThread bean被初始化为非单例,因为我需要多个实例。 InnerThread类还扩展了Thread。 有趣的是,这个错误出现在OuterThread中,它的设置方式与InnerThread完全相同。 我试图在下面包含所有相关的代码清单/堆栈跟踪。 有更多Spring体验的人可以告诉我这里发生了什么吗? 代码/配置清单 OuterThread.java片段: public class OuterThread extends Thread { private Queue createInnerThreads() { Queue threads = new ArrayBlockingQueue(); ApplicationContext ctx = SpringContextFactory.getApplicationContext(); int i […]

Maven:编译包含Java 1.6源的aspectj项目

主要问题 我想做的事情相当容易。 或者你会想。 但是,没有任何工作正常。 要求:使用maven,使用AspectJ编译器编译Java 1.6项目。 注意:我们的代码无法使用javac进行编译。 也就是说,如果未编入方面,则编译失败(因为我们有方面可以软化exception)。 2011年 2月21日更新:有两个同样可行的解决方案(两种情况都使用aspectj-maven-plugin与maven-compiler-plugin结合 ): 将false到编译器插件中(感谢Pascal Thivent ) 将process-sources到aspectj编译器插件中(感谢Andrew Swan ) 有关这些解决方案的更多信息,请参阅答案部分。 我认为解决方案#2是更好的方法。 相关问题 问题(基于以下失败的尝试): 如何让maven运行aspectj:直接编译目标,而不运行compile:compile? 你怎么忽略编译的失败:编译? 你如何指定一个指向你自己的ajc编译器的自定义compilerId(即make compile:compile使用除了plexus之外的aspectj编译器)?* 感谢您的任何建议。 这些是我尝试过的导致我的问题/问题的事情: 尝试1(失败):指定aspectJ作为maven-compiler-plugin的编译器: org.apache.maven.plugins maven-compiler-plugin 2.2 1.6 1.6 aspectj org.codehaus.plexus plexus-compiler-aspectj 1.8 这失败并出现错误: org.codehaus.plexus.compiler.CompilerException: The source version was not recognized: 1.6 无论我使用的是什么版本的plexus编译器(1.8,1.6,1.3等),这都行不通。 我实际上阅读了源代码,发现这个编译器不喜欢Java 1.5以上的源代码。 尝试2(失败):使用附加到编译和测试编译目标的aspectJ-maven-plugin: org.codehaus.mojo aspectj-maven-plugin 1.3 1.6 1.6 […]

使用Spring IoC和JavaConfig配置AspectJ方面?

根据Spring的文档使用Spring IoC配置AspectJ方面以配置Spring IOC的方面,必须在xml配置中添加以下内容: 正如@SotiriosDelimanolis所建议的那样,在JavaConfig中将以下内容重写为: @Bean public com.xyz.profiler.Profiler profiler() { com.xyz.profiler.Profiler profiler = com.xyz.profiler.Profiler.aspectOf(); profiler.setProfilingStrategy(jamonProfilingStrategy()); // assuming you have a corresponding @Bean method for that bean return profiler; } 但是,如果Profiler方面是用native aspectj .aj语法编写的,这似乎只能起作用。 如果它是用Java编写的并使用@Aspect注释,则会收到以下错误消息: 对于类型Profiler,方法aspectOf()未定义 对于使用@AspectJ语法编写的方面,是否有使用JavaConfig编写此方法的等效方法?

关于带注释控制器的Spring AOP建议

我试图使用AOP在带注释的控制器之后进行一些处理。 一切都在运行,没有错误,但建议没有被执行。 这是控制器代码: @Controller public class HomeController { @RequestMapping(“/home.fo”) public String home(ModelMap model) { model = new ModelMap(); return “home”; } } 和application-config中的设置 和实际的建议 public class TestAdvice implements AfterReturningAdvice { protected final Log logger = LogFactory.getLog(getClass()); public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { logger.info(“Called after returning advice!”); } } […]