java Spring Aop“Around.class”类未找到异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25861105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Aop "Around.class" Class Not Found Exception
提问by Haris Mehmood
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1040)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:220)
at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:615)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:465)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.springaoppractice.aop.AopMain.main(AopMain.java:12)
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: org/aspectj/lang/annotation/Around
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1088)
... 13 more
Caused by: java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.<clinit>(ReflectiveAspectJAdvisorFactory.java:74)
at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.<init>(AnnotationAwareAspectJAutoProxyCreator.java:53)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 15 more
I can not figure out why am I getting this error my AspectJ Lib includes following jars:
我不知道为什么会出现此错误,我的 AspectJ Lib 包含以下 jar:
- aspectjrt.jar
- aspectjweaver.jar
- cglib-2.2.2.jar
- aopalliance-1.0.jar
- asm-common-3.3.1.jar
- aspectjrt.jar
- aspectjweaver.jar
- cglib-2.2.2.jar
- aopalliance-1.0.jar
- asm-common-3.3.1.jar
Here is my main method:
这是我的主要方法:
package com.springaoppractice.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.springaoppractice.aop.service.ShapeService;
public class AopMain {
public static void main (String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
ShapeService shapeService = context.getBean("shapeService", ShapeService.class);
System.out.println(shapeService.getCircle().getName());
}
}
Here is my Aspect:
这是我的方面:
package com.springaoppractice.aop.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
@Before("execution(public String getName())")
public void LoggingAdvice (){
System.out.println("Logging advice initiazlied, get method is called!");
}
}
Heres my ServiceClass:
这是我的服务类:
package com.springaoppractice.aop.service;
import com.springaoppractice.aop.model.Circle;
import com.springaoppractice.aop.model.Triangle;
public class ShapeService {
private Circle circle;
private Triangle triangle;
public Circle getCircle() {
return circle;
}
public void setCircle(Circle circle) {
this.circle = circle;
}
public Triangle getTriangle() {
return triangle;
}
public void setTriangle(Triangle triangle) {
this.triangle = triangle;
}
}
Here are my model:
这是我的模型:
package com.springaoppractice.aop.model;
public class Circle {
private String name;
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.springaoppractice.aop.model;
public class Triangle {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Here is my SPRING.XML
这是我的 SPRING.XML
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<aop:aspectj-autoproxy/>
<bean name="triangle" class="com.springaoppractice.aop.model.Triangle">
<property name="name" value="TRIANGLE NAME" />
</bean>
<bean name="circle" class="com.springaoppractice.aop.model.Circle">
<property name="name" value="CIRCLE NAME" />
</bean>
<bean name="shapeService" class="com.springaoppractice.aop.service.ShapeService" autowire="byName" />
<bean name="loggingAspect" class="com.springaoppractice.aop.aspect.LoggingAspect" />
</beans>
Jars that are added to classpath:
添加到类路径的罐子:
回答by Naseer
So I was having the same issue due to incorrect aopalliance jar. What I had earlier was aopalliance-alpha1.jar
因此,由于 aopalliance jar 不正确,我遇到了同样的问题。我之前有的是 aopalliance-alpha1.jar
But then I changed it to aopalliance-1.0.jar which you can download from http://mvnrepository.com/artifact/aopalliance/aopalliance/1.0
但后来我把它改成了 aopalliance-1.0.jar,你可以从http://mvnrepository.com/artifact/aopalliance/aopalliance/1.0下载
The program is working for me.
该程序正在为我工作。
回答by Yegor Dovganich
Had the same error. I downloaded aopalliance-1.0-sources.jar
and aspectjweaver-1.7.1-sources.jar
instead of aopalliance-1.0.jar
and aspectjweaver-1.7.1.jar
(without "source"). After downloading proper jars (@kriegaex links) it works well.
有同样的错误。我下载了 aopalliance-1.0-sources.jar
andaspectjweaver-1.7.1-sources.jar
而不是aopalliance-1.0.jar
and aspectjweaver-1.7.1.jar
(没有“源”)。下载适当的罐子(@kriegaex 链接)后,它运行良好。
回答by Sandeep Tengale
Thanks for this question, I encounterd exactly the same error, its purely the Jar error. I resolved this issue by using asm-3.3.1.jar, aspectjrt.jar, cglib-3.1.jar, aopalliance-1.0.jar and aspectweaver-1.7.2.jar. I think its aopalliance and aspectweaver dependency which causes this error. Hope its helpful
感谢这个问题,我遇到了完全相同的错误,纯粹是 Jar 错误。我通过使用 asm-3.3.1.jar、aspectjrt.jar、cglib-3.1.jar、aopalliance-1.0.jar 和 aspectweaver-1.7.2.jar 解决了这个问题。我认为它的 aopalliance 和 aspectweaver 依赖导致了这个错误。希望它有帮助
回答by Sandeep Tengale
I had the same problem and im sure you are following java brains as i am :) just change the aopalliance -jar to aopalliance1.0-jar it works for me.
我遇到了同样的问题,我确定您正在关注我的 Java 大脑:) 只需将 aopalliance -jar 更改为 aopalliance1.0-jar 它对我有用。
回答by kriegaex
For me it works nicely with this on the classpath (I did not use Maven on purpose and manually added all libs in Eclipe in order to replicate your setup):
对我来说,它在类路径上很好地工作(我没有故意使用 Maven 并在 Eclipe 中手动添加所有库以复制您的设置):
The aspect's output appears on the console as expected. Maybe you want to compare your libs to mine. I strongly recommend to use Maven as a build tool though. This way you have more control over your dependencies.
方面的输出按预期显示在控制台上。也许您想将您的库与我的库进行比较。我强烈建议使用 Maven 作为构建工具。这样您就可以更好地控制您的依赖项。
Edit:I forgot to mention that my Eclipse project is an AspectJ project, not a normal Java project. I.e. I use ajcas a compiler, not javac. The result is compile-time weaving. If you want to use load-time weaving, please tell me and I will test it that way.
编辑:我忘了提到我的 Eclipse 项目是一个 AspectJ 项目,而不是一个普通的 Java 项目。即我使用ajc作为编译器,而不是javac。结果是编译时编织。如果您想使用加载时编织,请告诉我,我会以这种方式进行测试。
Edit 2:I have just converted my project to a normal Java project, i.e. javaccompiles all classes/aspects and Spring AOP with LTW are now used to apply aspects. It still works flawlessly. My current library setup looks like this:
编辑 2:我刚刚将我的项目转换为一个普通的 Java 项目,即javac编译所有类/方面,现在使用带有 LTW 的 Spring AOP 来应用方面。它仍然可以完美运行。我当前的库设置如下所示: