Question

I am using a fully no-XML Spring setup, and I am successfully using @EnableAspectJAutoProxy with my @Configuration class and finding my aspect classes with a combination of @Aspect and @Component.

But I have come to a point where I need to on-demand weave/enhance instances that were not created with the spring context, but all I can find is how to do it with ProxyFactory. But then I need to manually add advices with ProxyFactory.addAdvice(..), which I've already written once with (for example) @Before.

I don't want to rewrite those again.

Is there a way to get a reference to the factory that is (I guess?) created internally, by using the @EnableAspectJAutoProxy annotation? So that I could do something like:

@Autowired
private AspectJAutoProxyInstanceFactory f; // made up class, of course
[...]
Object bean = f.weave(obj);

Or instantiate a factory that can figure out what advices are already out there in my application:

// Also a made up class, of course.
ApplicationContextAwareProxyFactory f = new ApplicationContextAwareProxyFactory(applicationContext);
Object bean = f.weave(obj);

I've tried to look around but can't seem to find an answer. Maybe I'm just not good enough at looking. Thanks in advance, if you can help me!

Was it helpful?

Solution

I hope i understood the question right.

To achieve that you can use compile time weaving without spring. It's much more powerful then spring's proxy based approach and you don't have to change the aspects because spring already borrowed the @Aspect annotation from AspectJ.

You can easily achieve that with maven and aspectj-maven-plugin

Here is an example of the configuration:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.4</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
              <goal>test-compile</goal>  <!-- use this goal to weave all your test classes -->
            </goals>
          </execution>
        </executions>
      </plugin>

If you want to weave code that is in third party jar configure it like that:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.4</version>
        <configuration>
          <weaveDependencies>
            <weaveDependency>
              <groupId>org.agroup</groupId>
              <artifactId>to-weave</artifactId>
            </weaveDependency>
            <weaveDependency>
              <groupId>org.anothergroup</groupId>
              <artifactId>gen</artifactId>
            </weaveDependency>
          </weaveDependencies>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top