Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 980
Ula Krukar Avatar asked Mar 18 '10 13:03

Ula Krukar


People also ask

How do I enable aspect in Spring?

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.

What type of aspect weaving is supported by Spring AOP?

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.

What is aspect in Spring AOP?

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.

Can we use @bean annotation at class level?

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.


1 Answers

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).

like image 117
axtavt Avatar answered Nov 15 '22 06:11

axtavt