1
votes

How can I inject beans into a real AspectJ aspect using an annotation based application context?
I've created an aspect with AspectJ that implements ApplicationContextAware to access spring beans. Whenever the new-operator of a specified class is invoked, the aspect should inject a spring bean into the newly created instance.
To inject the application context into the aspect, Spring needs a handle to this aspect. The aspect itself is not created by spring but by the AspectJ runtime.
In an xml based application context one can achieve this by

<bean class="SomeAspectClass"
    factory-method="aspectOf">
</bean>

But how can I do this in an annotation based context?
My current workaround is to provide an xml based context that contains just this bean definition and merge it into my annotation based application context. But I just want to solve this with my annotation based context.

1

1 Answers

0
votes

As avery aspect has a static method 'aspectOf' to access its unique instance, just use reflection to invoke 'aspectOf' and get this instance.