Wednesday, September 26, 2012

Spring - java.lang.ClassNotFoundException: org.aopalliance.aop.Advice

Even after adding all Spring 3.1 jar i was getting excpetion as below :

Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException:
Caused by: java.lang.ClassNotFoundException: org.aopalliance.aop.Advice
    at$ Source)
    at Method)
    at Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 49 more
Solution :

As part of, Removed AOP Alliance interfaces from spring-aop.jar and
the aopalliance.jar needs to be added explicitly. Instead of using jar from any other sources, it's better to use jar from official maven repository.

Goto Central repository and search for org.aopalliance.aop.Advice in advance search by class name.
After adding aopalliance-1.0.jar, issues is resolved. And i am able to use MethodBeforeAdvice in terface. 

Author : Anuj Patel
Blog :


Benek said...

Thanks you for this post. It was very helpful

Batoul Hocine said...

thx for the tuto is so helpful
please how i can intercept call of method using AOP alliance interceptor:
i have class Hello with method "sayHello"; and i would tracing call of this method, code of tracing is like this:
public class TracingInterceptor implements MethodInterceptor {
Object target;

public Object invoke(MethodInvocation invocation) throws Throwable {

long start = System.nanoTime();

try {
return invocation.proceed();
finally {
"Invocation of method %s() with parameters %s took %.1f ms.",
(System.nanoTime() - start) / 1000000.0));
after what i do in the main for the call of the method sayHello will be intercepted ?
please i need this information pleaase

Anuj Patel said...

1. You need to use define bean for Your HelloService and bean for TracingInterceptor in Spring-bean.xml.
2.Then you should use Spring org.springframework.aop.framework.ProxyFactoryBean and pass property target as HelloService and interceptorNames as beanId of Class which will do hijack stuff.
3. In You Main method, Load Spring-bean.xml using ClassPathXmlApplicationContext and get HelloService Bean and call Your Method.

Ex.HijackMethod class is eq to your Tracking Interceptor
<bean id="customerService" class="com.anuj.spring.aop.methodInteceptor.CustomerService">
<property name="customerId" value="1"/>
<property name="customerName" value="anuj"/>

<bean id="hijackMothod" class="com.anuj.spring.aop.methodInteceptor.HijackMothod">

<bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService"/>
<property name="interceptorNames">
Main Method :
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"spring_aop_inteceptor.xml"});
CustomerService customerService = (CustomerService) context.getBean("customerServiceProxy");

Post a Comment