Showing posts with label aspectj. Show all posts
Showing posts with label aspectj. Show all posts

Saturday, March 21, 2020

Aspects to log enter and exit method

https://github.com/vernetto/logaspect

The idea is:

use @EnableAspectJAutoProxy in a @Configuration

create an annotation EntryExitLogger to mark the method(s) you want to be logged for entry/exit

have a @Component annotated with @Aspect and inside it you specify the aspects:

@Around("@annotation(EntryExitLogger)")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable{
}


That's all.

Detailed post on the topic https://medium.com/@wkrzywiec/moving-into-next-level-in-user-log-events-with-spring-aop-3b4435892f16

Doc of AspectJ here https://docs.spring.io/spring/docs/2.0.x/reference/aop.html

Good Aspectj cookbock here https://blog.espenberntsen.net/2010/03/20/aspectj-cheat-sheet/

Tuesday, December 27, 2011

WebLogic and AspectJ

Zillions of times I have wished in my life to be able to tell which methods are being executed in an application deployed in WLS, and the values of the parameters and return values. Call it "tracing method execution in weblogic".
If no debug statements exist in the WLS code or in the application, the only way is to weave the classes with some AOP product.

So here I go:

download AspectJ 1.7 http://www.eclipse.org/aspectj/

download WLS 12 http://www.oracle.com/technetwork/middleware/ias/downloads/wls-main-097127.html


about load-time weaving with a premain function using javaagent
http://www.eclipse.org/aspectj/doc/next/devguide/ltw-configuration.html

In setDomainEnv there is a property JAVA_PROFILE
I set it to
-javaagent:C:/apps/aspectj/lib/aspectjweaver.jar

after checking the WLS classpath, I see that the . directory (C:\Oracle\Middleware\user_projects\domains\base_domain) is in the classpath (because I have a System Variable CLASSPATH=.)
I create then a C:\Oracle\Middleware\user_projects\domains\base_domain\META-INF\aop.xml file where I define an INLINE ASPECT (no need to precompile separately with ajc):


Here is the aspect, to be compiled and put in the classpath (you will need to add the AspectJ jars to the Eclipse project)

package com.pierre.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


@Aspect
public abstract class AbstractAspect {


@Before("execution(* *(..))")
public void before(JoinPoint jp) {
System.out.println("CIAOOOOO");
}

@Pointcut("execution(public * *(..))")
public void anyPublicOperation() {
System.out.println("anyPublicOperation");
} 
}

and this is the aop.xml:

<aspectj>

<aspects>

<!-- define a concrete aspect inline -->
<concrete-aspect name="com.pierre.aop.MyTracing" extends="com.pierre.aop.AbstractAspect">
<!--pointcut name="tracingScope" expression="within(com.pierre.*)"/-->
</concrete-aspect>

<!-- Of the set of aspects declared to the weaver
use aspects matching the type pattern "com.pierre..*" for weaving. -->
<include within="com.pierre..*"/>


</aspects>

<weaver options="-verbose">
<!-- Weave types that are within the javax.* or org.aspectj.*
packages. Also weave all types in the foo package that do
not have the @NoWeave annotation. -->
<include within="com.pierre.*"/>

<!-- Dump all types within the "somepack" package,
both before are after they are woven,
to the "./_ajdump" folder on disk (for diagnostic purposes) -->
<dump within="com.pierre.*" />
</weaver>

</aspectj>

for some reasons, it doesn't work....
I can see in the stdout:


[JspClassLoader@72d4e5] info AspectJ Weaver Version 1.7.0.M1 built on Friday Dec 16, 2011 at 16:56:09 GMT
[JspClassLoader@72d4e5] info register classloader weblogic.servlet.jsp.JspClassLoader@72d4e5
[JspClassLoader@72d4e5] info using configuration /C:/Oracle/Middleware/user_projects/domains/base_domain/META-INF/aop.xml
[JspClassLoader@72d4e5] info define aspect com.pierre.aop.MyTracing
[ChangeAwareClassLoader@f45732] info AspectJ Weaver Version 1.7.0.M1 built on Friday Dec 16, 2011 at 16:56:09 GMT
[ChangeAwareClassLoader@f45732] info register classloader weblogic.utils.classloaders.ChangeAwareClassLoader@f45732
[ChangeAwareClassLoader@f45732] info using configuration /C:/Oracle/Middleware/user_projects/domains/base_domain/META-INF/aop.xml
[ChangeAwareClassLoader@f45732] info define aspect com.pierre.aop.MyTracing



My JSP does:

AOPCUT aopcut = new AOPCUT();
aopcut.hello("pierre");

where the AOPCUT class is

package com.pierre.aop;

public class AOPCUT {

public void hello(String message) {
System.out.println("Hello " + message);
}

}


and the interceptor gets called....