Spring Aop VS Aspectj

Spring AOP vs AspectJ

Spring-AOP Pros

  • It is simpler to use than AspectJ, since you don't have to use LTW (load-time weaving) or the AspectJ compiler.

  • It uses the Proxy pattern and the Decorator
    pattern

Spring-AOP Cons

  • This is proxy-based AOP, so basically you can only use method-execution joinpoints.
  • Aspects aren't applied when calling another method within the same class.
  • There can be a little runtime overhead.
  • Spring-AOP cannot add an aspect to anything that is not created by the Spring factory

AspectJ Pros

  • This supports all joinpoints. This means you can do anything.
  • There is less runtime overhead than that of Spring AOP.

AspectJ Cons

  • Be careful. Check if your aspects are weaved to only what you wanted to be weaved.
  • You need extra build process with AspectJ Compiler or have to setup LTW (load-time weaving)

Spring AOP and AspectJ on same method

Your interceptor does not proceed correctly. Please read the MethodInterceptor javadoc. The interceptor should look like this:

public class SAOPInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("Number of parameters " + methodInvocation.getArguments().length);
return methodInvocation.proceed();
}
}

Besides, your aspect calculates time wrong, too. First, you say finish = System.currentTimeMillis() - start and later you print start - finish. Either you should subtract finish - start or calculate the time spent in the variable, but not both and not start - finish. Why not simply System.out.println("Method execution time: " + (System.currentTimeMillis() - start));?

Spring AOP: Xml vs AspectJ approach

It sounds like you are trying to decide between Spring AOP and AspectJ, but you're assuming that Spring AOP requires XML-based configuration. It doesn't. You can use AspectJ annotations for both Spring AOP and AspectJ:

package com.example.app;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;

@Aspect
public class NotificationAspect {
@Autowired private NotificationGateway notificationGateway;

@Pointcut("execution(* com.example.app.ItemDeleter.delete(com.example.app.Item))")
private void deleteItemOps() { }

@AfterReturning(pointcut = "deleteItemOps() && args(item)")
public void notifyDelete(Item item) {
notificationGateway.notify(item, ConfigManagementEvent.OP_DELETE);
}
}

So if you're trying to compare Spring AOP and AspectJ, it's more sensible to compare AspectJ to annotation-based Spring AOP.

Spring AOP is generally simpler (you don't need the AspectJ compiler); hence the reference docs recommend Spring AOP over AspectJ unless you need more exotic pointcuts.

UPDATE: Responding to the OP's comment below, we can use XML configuration to advise specific methods:

<aop:config>
<aop:pointcut
id="deleteItemOps"
expression="execution(* com.example.app.ItemDeleter.delete(com.example.app.Item))" />
<aop:advisor
advice-ref="notificationAdvice"
pointcut-ref="deleteItemOps() && args(item)" />
</aop:config>

If you want to embed the pointcut right in the <aop:advisor>, you can do that too:

<aop:config>
<aop:advisor
advice-ref="notificationAdvice"
pointcut="execution(* com.example.app.ItemDeleter.delete(com.example.app.Item)) && args(item)" />
</aop:config>

(I haven't checked the && args(item) part of the XML configuration, but I think that's OK for the example I gave. If it doesn't work, try removing it and feel free to edit the answer accordingly.)

How does Spring Core make use of Spring AOP?

One of the reasons why Spring AOP is not used compared to AspectJ is
that Spring AOP cannot add an aspect to anything that is not created
by the Spring factory. What does this mean? Isn't everything created
from the Spring Factory?

In short, that Spring builds proxies for the beans in the Container, and relyies on these proxies to implement AOP.

Regarding the comparison with AspectJ:

From the Spring.io reference docs:

"
...

Thus, for example, the Spring Framework’s AOP functionality is normally used in conjunction with the Spring IoC container. Aspects are configured by using normal bean definition syntax (although this allows powerful “auto-proxying” capabilities). This is a crucial difference from other AOP implementations. You cannot do some things easily or efficiently with Spring AOP, such as advise very fine-grained objects (typically, domain objects). AspectJ is the best choice in such cases. However, our experience is that Spring AOP provides an excellent solution to most problems in enterprise Java applications that are amenable to AOP.

Spring AOP never strives to compete with AspectJ to provide a comprehensive AOP solution. We believe that both proxy-based frameworks such as Spring AOP and full-blown frameworks such as AspectJ are valuable and that they are complementary, rather than in competition. Spring seamlessly integrates Spring AOP and IoC with AspectJ, to enable all uses of AOP within a consistent Spring-based application architecture. This integration does not affect the Spring AOP API or the AOP Alliance API. Spring AOP remains backward-compatible. See the following chapter for a discussion of the Spring AOP APIs."

What is meant by "Spring AOP is proxy-based"?
How does Spring Core use the Spring AOP module "behind the scenes"?

Proxy based means that, beans are wrapped in another object (the proxy) which itercepts the calls to the obejct and can act upon that interception before calling the real method on the wrapped oject.

There are two ways of implementing this, one is using java Dynamic Proxys (Reflection), the other is using CGLIB, a library that adds the proxy capability at bytecode level.

Spring.io reference docs AOP

An article about Proxys

Spring AOP vs Aspecj

Your first point simply conveys that you can only apply point-cuts on method level, field interception is not implemented in spring-aop.

Next point tells that you cannot add advices on domain-objects(which are simple pojo entities),

The last is about weaving ,weaving is wiring of Aspects into objects in the spring XML file in the way as JavaBean. OR simply say, weaving is about adding new bytecode to your java class to make it usable to the framework.

Usage question: Spring AOP vs. AspectJ weaving

Depending on whether or not you already use AspectJ in your project, you might find the following approach simpler:

Wrap your ComboPooledDataSource instance in a non-final decorator class instance; cglib should be able to proxy the decorator without being any the wiser.

Assuming - for the example's sake - that the dataSource attribute from your SampleDaoImpl class is of type javax.sql.DataSource:

public class ComboPooledDataSourceDecorator implements javax.sql.DataSource {

private ComboPooledDataSource delegate;

public void setDelegate(ComboPooledDataSource delegate) {
this.delegate = delegate;
}

public Connection getConnection() {
return delegate.getConnection();
}

// other methods of javax.sql.DataSource

}

Wire it into spring like this:

<bean id="sample_data_source" class="mystuff.ComboPooledDataSourceDecorator">
<property name="delegate">
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
...
</bean>
</property>
</bean>

AOP vs Spring Security

Check here

Aspect declarations are supported by the
org.aspectj.lang.annotation.Aspect annotation. The declaration:

 @Aspect
public class Foo {}

Is equivalent to:

 public aspect Foo {}

NOTE: The first one is detected by spring. The later requires AspectJ.

And for the second question. The comparison is impossible. Because the first one is a framework and the later is a paradigm. Spring security uses AOP to secure method calls, AOP by itself is not a security mechanism. Unless of course, you are going to build your own security using AOP, which is re-inventing the wheel.



Related Topics



Leave a reply



Submit