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.
For using Spring AOP in Spring beans, we need to do the following: Declare AOP namespace like xmlns:aop=“https://www.springframework.org/schema/aop” Add aop:aspectj-autoproxy element to enable Spring AspectJ support with auto proxy at runtime. Configure Aspect classes as other Spring beans.
Weaving can be done at compile time, at load time, or at runtime. There are two ways in which classes and aspects can be woven: static or dynamic. Spring AOP does dynamic weaving of aspects by creating proxy of the target objects.
In Spring AOP, aspects are implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style). Join point: a point during the execution of a program, such as the execution of a method or the handling of an exception.
It decouples the declaration of the bean from the class definition and lets you create and configure beans exactly how you choose. With @Bean you aren't placing this annotation at the class level. If you tried to do that you would get an invalid type error.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With