[英]Spring throws NoClassDefFoundError when instantiating a bean
I have a spring bean. 我有一个四季豆。
@Component
public class Omg {
public void omg() throws IOException {
HttpServletRequest request = null;
InputStream is = request.getInputStream();
is.read();
}
}
I am not using it in a servlet container. 我没有在servlet容器中使用它。 This is just for the sake of example.
这只是为了举例。
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
This line throws exception java.lang.NoClassDefFoundError: javax/servlet/ServletInputStream.
该行抛出异常
java.lang.NoClassDefFoundError: javax/servlet/ServletInputStream.
Servlet api is not in classpath in runtime. Servlet API在运行时不在类路径中。 Okay, I understand that
NoClassDefFoundError
should be thrown when omg method runs. 好的,我知道在运行omg方法时应该抛出
NoClassDefFoundError
。 The question is: why is it thrown when spring tries to instantiate the bean and why ServletInputStream
rather than HttpServlerRequest
? 问题是:为什么在spring尝试实例化bean时抛出该异常,为什么使用
ServletInputStream
而不是HttpServlerRequest
?
Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/ServletInputStream
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2532)
at java.lang.Class.getDeclaredConstructors(Class.java:1901)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:234)
UPDATE: However this works fine 更新:但这很好
public class A {}
public class B {
public void method() {
A a = new A();
}
}
public class C {
public static void main(String[] args) {
B b = new B();
}
}
compile all three classes. 编译所有三个类。 remove A.class.
删除A.class。 run main method of C class.
运行C类的主要方法。 It works fine.
工作正常。
public class NoClassDefFoundError extends LinkageError
公共类NoClassDefFoundError扩展LinkageError
Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression ) and no definition of the class could be found.
如果Java虚拟机或ClassLoader实例尝试加载类的定义(作为常规方法调用的一部分或使用new表达式创建新实例的一部分 )而抛出,则找不到该类的定义。
public class A {}
public class B {
public void m(A a) {
}
public static void main(String[] args) {
B b = new B();
}
}
javac A.java // OK
javac B.java // OK
// remove A.class
java B
Exception in thread "main" java.lang.NoClassDefFoundError: A
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.