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 java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown 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 https://jira.springsource.org/browse/SPR-2011, 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 http://search.maven.org/#browse 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 : http://goldenpackagebyanuj.blogspot.in/

3 comments:

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;

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {

long start = System.nanoTime();

try {
return invocation.proceed();
}
finally {
System.out.println(
String.format(
"Invocation of method %s() with parameters %s took %.1f ms.",
invocation.getMethod().getName(),
Arrays.toString(invocation.getArguments()),
(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>

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

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

Post a Comment