I want catch all Exception logs for AOP with best performance - performance

I heaard that spring AOP is slower 30 times than aspectj.
So, I insert below code in the spring context xml.
<aop:aspectj-autoproxy />
so, is aspectj applied to?
plz let me know and any idea that can substitute aop for better performance!!

<aop:aspectj-autoproxy/>
Just enables #AspectJ annotation support .The AOP runtime is still pure Spring AOP . See the following from spring reference.
#AspectJ refers to a style of declaring aspects as regular Java classes annotated with
Java 5 annotations. The #AspectJ style was introduced by the AspectJ project as part of
the AspectJ 5 release. Spring 2.0 interprets the same annotations as AspectJ 5, using a
library supplied by AspectJ for pointcut parsing and matching. The AOP runtime is still
pure Spring AOP though, and there is no dependency on the AspectJ compiler or weaver.
If you want to use Aspectj compiler/weaver , check here .

Related

Spring #Transactional - synchronize via AspectJ

I am trying to synchronize declarative transactions (i.e. methods annotated with #Transactional) using AspectJ like so:
...
import org.aspectj.lang.annotation.Aspect;
...
#Component
#Aspect
public class TransactionMonitor extends TransactionSynchronizationAdapter {
#Before("execution(#org.springframework.transaction.annotation.Transactional * *.*(..))")
private void registerTransactionSynchronizationOnAnnotation(JoinPoint joinPoint) {
TransactionSynchronizationManager.registerSynchronization(this);
}
}
This currently fails with java.lang.IllegalStateException: Transaction synchronization is not active which indicates that the synchronization is not run inside the transaction execution, but before. I want to ensure that this is the other way round, of course.
I found this answer, however #Order(Ordered.LOWEST_PRECEDENCE) had no effect, and
#DeclarePrecedence(
"org.springframework.transaction.aspectj.AnnotationTransactionAspect, xxx.xxx.TransactionMonitor, *"
)
led to this during startup:
java.lang.IllegalArgumentException: DeclarePrecedence not presently supported in Spring AOP
I have the feeling this is AOP and AspectJ not being happy with each other, but I am not sure. I am thankful for any ideas.
EDIT: I have to use #EnableTransactionManagement(proxyTargetClass = true), can this be related to the issue?
For #DeclarePrecedence you need to switch to native AspectJ. Spring AOP is just "AOP lite" and technologically has little in common with AspectJ other than its syntax which is basically an AspectJ subset. The Spring manual describes how to use native AspectJ in Spring via LTW (load-time weaving). Precedence declaration for Spring components rather works using #Order, BTW.
I am not a Spring user at all, but as for declarative transaction management, it already knows proxy-based Spring AOP versus native AspectJ mode, see EnableTransactionManagement.mode and the enum constants in AdviceMode. Besides, EnableTransactionManagement also has an order property. Reading Javadoc and the Spring manual helps, I guess.

Spring AOP pointcut expression syntax for wildcard

I'm currently using AspectJ 1.6.9 and i wonder why the following pointcut expression:
(execution (* it.dtt..endpoint.*..*.invoke*(..)))
doesn't match the execution of the method declared:
protected Object invokeInternal(Object object) throws Exception
of the class:
it.dtt.prova.endpoint.Richiesta
any idea?
"Due to the proxy-based nature of Spring's AOP , protected methods are by définition not intercepted".
you need to change the access modifier of that method to public or consider use the spring-driven native AspectJ weaving
Spring docs:
Due to the proxy-based nature of Spring's AOP framework, protected
methods are by definition not intercepted, neither for JDK proxies
(where this isn't applicable) nor for CGLIB proxies (where this is
technically possible but not recommendable for AOP purposes). As a
consequence, any given pointcut will be matched against public methods
only!
If your interception needs include protected/private methods or even
constructors, consider the use of Spring-driven native AspectJ weaving
instead of Spring's proxy-based AOP framework. This constitutes a
different mode of AOP usage with different characteristics, so be sure
to make yourself familiar with weaving first before making a decision.

How can I log private methods via Spring AOP?

I am not able to log the private methods using spring aop performance logging.
Below is the configuration I am using below configuration
<aop:config proxy-target-class="true">
<aop:pointcut id="allServiceMethods" expression="execution(* com.mycom.app.abc..*.*(..))"/>
<aop:advisor pointcut-ref="allServiceMethods" advice-ref="performanceMonitor" order="2"/>
</aop:config>
I am having cglib jar on my class path.
You have to use compile time weaving instead of the proxy usage for Spring AOP.
From Spring AOP - Supported Pointcut Designators
Due to the proxy-based nature of Spring’s AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn’t applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only!
If your interception needs include protected/private methods or even constructors, consider the use of Spring-driven native AspectJ weaving instead of Spring’s proxy-based AOP framework. This constitutes a different mode of AOP usage with different characteristics, so be sure to make yourself familiar with weaving first before making a decision.

How to advice entity classes not spring beans

i'm looking to advice setters of entity classes using AspectJ on Spring Boot, but i found that only spring beans could be advised.
Is there any trick to advice setters of entity classes (for example), these entity classes could not be spring beans.
only spring beans could be advised
Well, it's true in case you are using Spring AOP and not(!!!) AspectJ.
If replacing Spring AOP with AspectJ is an option, you can weave what ever you like by using #Configurable
Here
You can find the documentation that says that you can put Spring annotations like #Transactional on your non beans instances.

Using Spring AOP uses underneath aspectj?

Hy,
Reading a lot about Spring AOP vs AspectJ, I still have some doubts:
1.)When using Spring AOP with classes annotated with #Aspect and using "aop:aspectj-autoproxy" tag , it can be said that we are using just the annotations of aspectj or besides that it is being used AspectJ too for the weaving?
2) Its said that AspectJ has better performance because the weaving is in compilation time, it means that the target class files are physically changed inserting the aspects in them? is it not a bit aggressive?
3)It said that Spring uses proxys for AOP, so, I undertand that when you get a bean from Spring, Spring builds a proxy in memory that has already inserted the aspects in it, right?
So why is it said that when a method from your proxy bean calls other method in the proxy, the last method will not have aspects?
Thanks
1) using aspectj-autoproxy means that #Aspectannotations are recognized, but Spring proxies are still being created, see this quote from the documentation:
Do not be misled by the name of the element:
using it will result in the creation of Spring AOP proxies. The
#AspectJ style of aspect declaration is just being used here, but the
AspectJ runtime is not involved.
2) AspectJ supports load time weaving, byte code weaving and compile time weaving. There should no difference in performance, it's just a different point in time to weave the aspect in (compilation, third party jars available, class load time), see this answer for further details.
It is actually more transparent once it's set up to have the aspects weaved at these moments, with runtime proxies there are problems when a bean calls itself using this.someMethod, the aspects don't get applied because the proxies get bypassed (#Transactional/#Secured does not work, etc.).
3) Have a look at this picture from the documentation:
With runtime proxies (non AspectJ), Spring leaves the bean class untouched. What it does is it creates a proxy that either implements the same interface as the bean (JDK proxy), or if the bean implements no interface then it dynamically creates a proxy class with CGLIB (subclass of bean).
But in both cases a proxy is created that delegates the calls to the actual bean instance. So when the bean call this.methodB() from methodA, the proxy is bypassed because the call is made directly on the bean and not on the proxy.
Spring AOP can be configured with AspectJ-sytle, ie annotations are parsed to build configuration but AspectJ compiler is not used for weaving. Only a subset of AspectJ annotations and poincut definitions can be used with Spring AOP.
Maybe, but I don't know any class that has complained. However, is possible that some classes don't allow re-weaving when modified by other bytecode tools.
Inner calls are not proxied because they call on this.method() (with this = the target bean begin proxied) and not on proxy.method() so the proxy has no chances to intercept the call. However, Spring AOP proxies usually notices when a target method return this and return itself instead, so calls like builder.withColor(Color.RED).withHat(Hat.Cowboy) will work. Note that in Spring AOP there are always two classes involved: the proxy and the target.

Resources