Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Spring AOP or AspectJ

This is following on from this question:

Spring autowired bean for @Aspect aspect is null

My initial understanding was that when using Spring AOP, classes annotated with @Aspect are created as spring managed beans, so dependency injection would work as normal. However it seems that an object with the @Aspect annotation is created as a singleton outside the spring container, hence me having to configure it in XML like so in order to enable it as a spring managed bean:

<bean id="aspect" class="com.mysite.aspect" factory-method="aspectOf" />

This has now completely confused me. I thought the following configuration would use spring AOP:

<context:component-scan base-package="com.mysite.aspectPackage"/>
<aop:aspectj-autoproxy/>

So it would scan for @Aspect annotations using component-scan creating aspect beans, and then autoproxy would create a beanPostProcessor which proxies all beans within my context with the appropriate advice. I then thought to enable aspectJ I would need a completely different XML configuration (which incidentally I can't seem to find an example of in the documentation). It would be this configuration that uses aspectJ to create aspects that would be outside of my container or which work by manipulating bytecode rather than proxying.

Note
This is not a question on the difference between spring AOP and aspect J, this is well articulated here:

Spring AOP vs AspectJ

like image 312
mogronalol Avatar asked Mar 12 '12 12:03

mogronalol


People also ask

Does Spring AOP use AspectJ?

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.

How do I enable AOP 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.

Does AOP decrease performance?

As far as performance is concerned, compile-time weaving is much faster than runtime weaving. Spring AOP is a proxy-based framework, so there is the creation of proxies at the time of application startup. Also, there are a few more method invocations per aspect, which affects the performance negatively.


2 Answers

@Component will create 2 instances, one inside the Spring container, one inside the aspectJ container.

use @Configurable to allow spring to manage dependency injection etc. for your class when instantiated by the aspectj container.

@Aspect is an aspectj style annotation that is supported by spring-aop, where runtime weaving is used to handle interception etc.

Compile-time weaving allows you to disregard the use of as pointcuts will be present in the bytecode, this is done via the aspectj compiler (See https://www.mojohaus.org/aspectj-maven-plugin/ for mvn integration) .

Whether you use the aspectj compiler or spring-aop makes no difference, it wont create your aspect as a managed bean in the way you want unless you use factory / configurable.

Aspectj configuration is, strictly, the pointcut definitions etc that will be present inside your class.

like image 105
MikePatel Avatar answered Oct 19 '22 22:10

MikePatel


@Aspect is not a spring annotation, and it is not detected by component-scan. So you have to register it somehow as a spring bean. The aspectOf solution works. You can also try

@Aspect
@Component
like image 25
Bozho Avatar answered Oct 19 '22 22:10

Bozho