aspectj

Aspectj aspect for specifying multiple packages

隐身守侯 提交于 2019-12-03 15:38:07
I wanted to specify a pattern for aspectj @Around aspect that includes multiple packages. Example : package 1 : aaa.bbb.ccc.ddd package 2 : aaa.bbb.ccc.eee package 3 : aaa.bbb.ccc.eee.fff Pattern which i used : @Around("execution(* aaa.bbb.ccc.ddd.*.*(..)) && execution(* aaa.bbb.ccc.eee..*.*(..))") i.e Intercept packages aaa.bbb.ccc.ddd, aaa.bbb.ccc.eee and any sub-package of aaa.bbb.ccc.eee But this pattern doesnt seem to work. Though specifying a single pattern without && condition works. Can someone suggest whats wrong with this pattern? Thanks, Gayathri && stands for logical AND . What You

How to create an aspect on class, that is not a bean using Spring AOP?

橙三吉。 提交于 2019-12-03 15:25:26
I work on an legacy application, where Spring AOP (namely ProxyFactoryBean ) is used. I need to add an aspect around a method of a certain class. This class is not a bean however. The AspecjJ pointcut expression would be like this: execution(* xyz.package.Class.method()) I created a MethodInterceptor and AspectJExpressionPointcut , but I don't know how make those two work together. EDIT : I do not have source code for this class, it is a 3rd party library. The instances of this class are not created by me, neither in source code, nor in spring configuration as beans. It is used internally by

@Tansactional and @Aspect ordering

落花浮王杯 提交于 2019-12-03 15:04:22
问题 I would like to execute my code just before @Transactional transaction is started. @Aspect @Order(Ordered.HIGHEST_PRECEDENCE) //@Order(Ordered.LOWEST_PRECEDENCE) public class SynchronizerAspect { @Pointcut("execution(public * xxx.xxx.services.*.*(..))") private void anyServiceOperation() { } @Around("anyServiceOperation()") public Object synchronizerAdvice(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("Synchronizing : " + joinPoint.getSignature().getName()); return

AspectJ “around” and “proceed” with “before / after”

假如想象 提交于 2019-12-03 14:38:05
问题 Let's say you have three advices: around , before and after . 1) Are before/after called when proceed is called in the around advice, or are they called before/after the around advice as a whole? 2) If my around advice does not call proceed , will the before/after advice be run anyway? 回答1: With this Test @Aspect public class TestAspect { private static boolean runAround = true; public static void main(String[] args) { new TestAspect().hello(); runAround = false; new TestAspect().hello(); }

Can AspectJ replace “new X” with “new SubclassOfX” in third-party library code?

佐手、 提交于 2019-12-03 14:01:05
I am looking at AspectJ to see if perhaps we can use it in our test suite. We have a rather large third party Java communications library hardwired to use its own classes (which do not implement any interfaces) which in turn mean that we need a physical backend present and correctly configured to be able to run tests. I am looking at our options for removing this restriction. A possibility would be to create a subclass of the troublesome classes and then ask AspectJ to simply replace "new X" with "new OurSubclassOfX" when loading the third party library, but I am new to AspectJ and from my

Turning one annotation into many annotations with AspectJ

时光毁灭记忆、已成空白 提交于 2019-12-03 13:43:46
I have discovered a pattern in my JPA mappings that I would like to codify. A simple example follows: @OneToMany(fetch=FetchType.EAGER) @Sort(type=SortType.NATURAL) private SortedSet<Item> items; I would like to create a single annotation called SortedOneToMany that I can apply to the above set: public @interface SortedOneToMany { FetchType fetch() default EAGER; SortType sort() default NATURAL; Class comparator() default void.class; } I have written the following aspect to "attach" the JPA annotations whenever it sees my annotation: public aspect SortedOneToManyAspect { declare @field:

AspectJ load-time weaving for signed jars

五迷三道 提交于 2019-12-03 13:17:11
Does anybody success in using AspectJ load-time weaving with signed jars? I got an exception and have no idea how to fix it (tested with AspectJ 1.6.8-16.10): Exception in thread "main" java.lang.NoClassDefFoundError: com/package/clazz$AjcClosure1 at com.package.test.main(test.java:55) Caused by: java.lang.ClassNotFoundException: com.package.clazz$AjcClosure1 at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher

Does Spring AOP do compile time weaving or load time weaving?

谁说我不能喝 提交于 2019-12-03 13:12:17
问题 I am starting to use Spring AOP for a project and I am a little bit confused about weaving. I know that Spring AOP has a dependency on AspectJweaver.jar, but as the documentation says, this is not for the weaving, but just that it uses some of the classes from this jar. But my question is, if it is not using AspectJ for weaving, does Spring AOP have its own weaving and is it performed at load time or compile time? The relevant part of my Spring Configuration XML file is: <context:annotation

Spring AOP之同一个对象方法内部自调用导致事务失效问题

落花浮王杯 提交于 2019-12-03 12:12:17
对于像我这种喜欢滥用AOP的程序员,遇到坑也是习惯了,不仅仅是事务,其实只要脱离了Spring容器管理的所有对象,对于SpringAOP的注解都会失效,因为他们不是Spring容器的代理类,SpringAOP,就切入不了 当然可以使用原生ASPECTJ,不用SpringAOP,但是基于生态链问题,还是尽量使用SpringAOP 这里简单说一下,Spring如何选择使用CGLIB,或者是JDK代理, 简单来说,如果实现了某个接口,那么Spring就选择JDK代理(不一定),如果没有,那么就选择CGLIB代理,说起来简单,Spring还会闹脾气,不代理呢 问题引出 一直以来比较多的情况是在Controller 调用Service 的方法,把事务直接在Service的方法上,妥妥的没问题,事务正常执行 考虑以下问题: 同对象的方法B 调用自己的方法A,这里的事务将会失效(严格上来说,只要对方法A使用注解AOP均会失效),原因是因为这里的this.调用的并不是Spring的代理对象 @Service public class ClassA{ @Transactional(propagation = Propagation.REQUIRES_NEW) public void methodA(){ } /** * 这里调用methodA() 的事务将会失效 */ public void

Lombok and AspectJ

心已入冬 提交于 2019-12-03 11:24:13
Im trying to use Lombok in combination with AspectJ and Maven. So, what's the problem? When i use the AspectJ Maven Plugin (www.mojohaus.org/aspectj-maven-plugin/), it takes the sources and compiles them and ignores changes made by Lombok. I followed this tutorial and came up with this code and AspectJ works, but Lombok dies with this message: [WARNING] You aren't using a compiler supported by lombok, so lombok will not work and has been disabled. Your processor is: org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.BatchProcessingEnvImpl Lombok supports: sun/apple javac 1.6, ECJ So,