我正在阅读 this article关于基于 Spring Java 的配置。我正在创建一个类似于本教程中的示例,但我在构建过程中使用 Maven。
我在我的项目中添加了以下依赖项:spring-core
、spring-bean
、spring-context
、spring -context-support
和 spring-asm
。
问题是,当我尝试执行我的应用程序时,我收到以下错误消息:
feb 13, 2013 10:26:49 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4ba4536d: startup date [Wed Feb 13 10:26:49 CET 2013]; root of context hierarchy
Exception in thread "main" java.lang.IllegalStateException: CGLIB is required to process @Configuration classes. Either add CGLIB to the classpath or remove the following @Configuration bean definitions: [helloWorldConfig]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:327)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:222)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:620)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73)
at org.andrea.myexample.myJavaConfiguration.MainApp.main(MainApp.java:13)
在线阅读我了解到要使用这些注释(@Configuration
和 @Bean
)我必须添加 spring-asm
依赖项到我的项目。我试过了,但没用。
这是我的整个 pom.xml
文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.andrea.myexample</groupId>
<artifactId>myJavaConfiguration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>myJavaConfiguration</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
</dependencies>
有人可以帮助我吗?
最佳答案
在同一个教程页面中,它声明您需要在类路径中包含 CGLib jar(添加了强调):
Let us have working Eclipse IDE in place and follow the following steps to create a Spring application:
...
3 - Because you are using Java-based annotations, so you also need to add CGLIB.jar from your Java installation directory and ASM.jar library which can be downloaded from asm.ow2.org.
...
使用 Maven,您需要将以下依赖项添加到您的 pom.xml
中:
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
关于java - Spring Framework 中基于 Beans Java 的配置不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14851259/