I am making a functionality to add logging using Spring AOP using pointcuts. I have added a folder spring in META-INF where I've kept beans.xml which has the following code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy/>
<!--<aop:include name="servicesLogger"/>
</aop:aspectj-autoproxy> -->
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
<bean id="servicesLogger" class="com.app.services.core.logger.ServicesLogger"/>
The ServiceLogger class lies in a separate module that defines the pointcuts is as follows:
package com.app.services.core.logger;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Configurable;
@Configurable("servicesLogger")
@Aspect
public final class ServicesLogger {
private Logger log = Logger.getLogger(ServicesLogger.class);
private ServicesLogger() {
super();
}
@Before("execution(* *(..))")
public void beforeLog(JoinPoint point) {
System.out.println(point.getSignature().getName() + " called...");
log.info(point.getSignature().getName() + " called...");
}
@After("execution(* *(..))")
public void afterLog(JoinPoint point) {
System.out.println(point.getSignature().getName() + " called...");
log.info(point.getSignature().getName() + " called...");
}
@Around("execution(* *(..))")
public void aroundLog(JoinPoint point) {
System.out.println(point.getSignature().getName() + " called...");
log.info(point.getSignature().getName() + " called...");
}
}
When I try to deploy the module containing the beans.xml file then the servicemix is unable to create the bean and throws the following exception:
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory
I have tried installing the 4 jars: - spring-core.jar - spring-aop.jar - aspectjrt.jar - aspectjweaver.jar
into the servicemix as well as tried to provide it by putting them in lib folder of servicemix but to no avail.
Please guide me what I am missing in this. Thanks in advance.
I was able to resolve the issue by doing some research on osgi/servicemix. Servicemix provides a feature spring-aspects that is a system bundle number 72.
features:list
By this you will be able to view all the available features in karaf. Then to install the feature use the below command.
features:install spring-aspects
After installing this now you have provided the aop capability to the servicemix. Now will need 2 more jars:
-aspectjrt.jar
-aspertjweaver.jar
If you have a later version of the aspectjweaver then you do not need the first one as it is a subset of aspectjweaver.jar
Just install the above jars into your karaf if they are available in your .m2
Use the below commands to install these:
osgi:install -s wrap:mvn:org.aspectj/aspectjrt/1.6.11
osgi:install -s wrap:mvn:org.aspectj/aspectjweaver/1.6.11
An extra jar cglib.jar might be needed to be installed in some cases which can be done as follows:
osgi:install -s wrap:mvn:cglib/cglib/2.1_3
I was able to resolve the dependencies and remove the above errors by doing this.
Hope this helps somebody else as well. :)