我正在关注如何使用Spring的this教程,并根据提供的示例,我得到以下异常:
Exception in thread "main" java.lang.IllegalStateException: Cannot load configuration class: com.tutorialspoint.HelloWorldConfig
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:378)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:263)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:126)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at com.tutorialspoint.MainApp.main(MainApp.java:9)
Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:237)
at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
at org.springframework.context.annotation.ConfigurationClassEnhancer.createClass(ConfigurationClassEnhancer.java:128)
at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:100)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:368)
... 7 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:384)
at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:219)
... 12 more
Caused by: java.lang.SecurityException: class "com.tutorialspoint.HelloWorldConfig$$EnhancerBySpringCGLIB$$b5aece24"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(ClassLoader.java:952)
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:666)
at java.lang.ClassLoader.defineClass(ClassLoader.java:794)
... 18 more
我研究了我的问题并找到了this;有人也和我有同样的问题,它与确保ASM与CGLIB兼容有关。但是我尝试过这个解决方案而且没有用,我甚至使用了与提供的完全相同的版本(GBLIB 2.2.2和ASM 3.3.1)。
为了纠正这个问题,我需要做些什么?
为简单起见,以下是我使用的文件,这些文件是从提供的教程中提取的。
HelloWorldConfig.java
package com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld() {
return new HelloWorld();
}
}
HelloWorld.java
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void getMessage() {
System.out.println("Your Message : " + message);
}
}
MainApp.java
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
public class MainApp {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext ctx = new AnnotationConfigApplicationContext(
HelloWorldConfig.class);
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage("Hello World!");
helloWorld.getMessage();
}
}
同样通过说&#39;但是我已经尝试过这个解决方案并且它没有工作&#39;我的意思是返回完全相同的错误。
答案 0 :(得分:0)
所以,我会说&#34;其他&#34;你提到过,有一个不同的问题
即使在&#34; Last-Shown-Exception&#34;和你的一样。
但正如您在堆栈跟踪中看到的那样,&#34; source&#34;是 SecurityException 。
*无法加载配置类* -Error是后果
我认为&#34;代码签名&#34;有问题。在你的项目中 或者,由于ByteCode-Manipulation,标记被破坏了。
<强> PS 强>:
当你引用&#34; SignedLibs&#34;有时也会发生这种情况。 和&#34; UnsignedLibs&#34;在你的项目中。
在这种情况下,从签名的库中删除签名。
答案 1 :(得分:0)
该项目运行所需的所有jar:
1)org.springframework.core-3.0.1.RELEASE-A.jar
2)spring-context-3.0.4.RELEASE.jar
3)org.springframework.beans-3.0.1.RELEASE-A.jar
4)commons-logging-1.1.1.jar
5)asm-3.3.1.jar
6)cglib-2.2.2.jar
要获取这些jar,请直接将下载的jar添加到您的项目中,或者在pom.xml中提供以下依赖项以自动为您下载它们。
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
如果尚未存在,请将其添加到您的maven settings.xml文件中:
<profiles>
<profile>
<id>SPRINGLEARN</id>
<activation>
<jdk>1.8</jdk>
</activation>
<repositories>
<repository>
<id>thirdPartyRepo</id>
<name>Third party repository</name>
<url>https://repo.spring.io/libs-release/</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>thirdPartyPluginRepo</id>
<name>Third party plugin repository</name>
<url>https://repo.spring.io/libs-release/</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
之后,只需运行您的项目即可。
-右键单击您的项目->运行方式-> Maven clean
-右键单击您的项目->运行方式-> Maven安装
-右键单击您的项目->运行方式-> Java应用程序