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

13,828

Solution 1

You can use load-time weaving with full AspectJ support as described here, it doesn't require access to the source of classes being advised nor control over their instantiation (though it requires <context:load-time-weaver /> and presence of the weaver itself using -javaagent:... or other methods).

Solution 2

Try @Configurable. It is explained in this docs.

The @Configurable annotation marks a class as eligible for Spring-driven configuration

(you'd need <context:load-time-weaver />)

Update You can make a 3rd party component a bean by listing it in applicationContext.xml as <bean class=".." /> (you don't need @Configurable with that)

Share:
13,828
Ula Krukar
Author by

Ula Krukar

Software developer, Java and C#.Net, always keen to find out something more...

Updated on June 04, 2022

Comments

  • Ula Krukar
    Ula Krukar almost 2 years

    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 the library.

    Any help appreciated.