Java 为什么 Maven 构建无法解析 Spring @Bean @Configuration 等注释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18549289/
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
Why can't Maven build resolve Spring @Bean @Configuration etc annotations?
提问by DaFoot
I'm trying to move my Spring MVC project to 3.2.4.
我正在尝试将我的 Spring MVC 项目移动到 3.2.4。
When I attempt to run up the application in IntelliJ, using Maven as I had done previously, I am getting errors about not being able to find a series of annotation classes from Spring...
当我尝试在 IntelliJ 中运行应用程序时,使用 Maven 就像我以前所做的那样,我收到关于无法从 Spring 找到一系列注释类的错误......
@Bean
@Configuration
@ComponentScan
@PropertySource
are all failing with unable to resolve class error messages.
都因无法解决类错误消息而失败。
Other annotations are ok, which makes me think it is a dependency issue, as the Spring version is the main change in my project codebase.
其他注释还可以,这让我认为这是一个依赖问题,因为 Spring 版本是我项目代码库中的主要变化。
Has Spring 3.2.4 changed where those annotations live in the packaging?
Spring 3.2.4 是否更改了这些注释在包装中的位置?
My current pom (some bits removed for brevity):
我当前的 pom(为简洁起见删除了一些位):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
<!-- SNIP project naming etc, nothing changed from before moving to 3.2.4 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
<spring.version>3.2.4.RELEASE</spring.version><!-- 3.2.4 -->
<spring.security.version>3.2.0.RC1</spring.security.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency><!-- needed for freemarker FreeMarkerConfigurer stuff -->
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
<type>jar</type>
</dependency>
<!-- LOGGING -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.0.RELEASE</version>
<scope>runtime</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.5.8</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.8</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>runtime</scope>
</dependency>
<!-- Servlet Spec -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- view -->
<!-- SNIP sitemesh/freemarker/jsp etc -->
<!-- DB access -->
<!-- SNIP hibernate stuff, sql dialect/driver-->
<!-- Test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>bonecp-repo</id>
<name>BoneCP Repository</name>
<url>http://jolbox.com/bonecp/downloads/maven</url>
</repository>
<!--<repository>-->
<!--<id>springsource-milestones</id>-->
<!--<name>SpringSource Milestones Proxy</name>-->
<!--<url>https://oss.sonatype.org/content/repositories/springsource-milestones-->
<!--</url>-->
<!--</repository>-->
<!--<repository>-->
<!--<id>jboss-public-repository-group</id>-->
<!--<name>JBoss Public Repository Group</name>-->
<!--<url>http://repository.jboss.org/nexus/content/groups/public</url>-->
<!--</repository>-->
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<path>/yhj</path>
</configuration>
</plugin>
</plugins>
</build>
采纳答案by superEb
There is a duplicate dependency of org.springframework:spring-context
in your POM that is probably causing classpath conflicts. The first one is a compile dependency using version 3.2.4.RELEASE
, and the second one is a runtime dependency using version 3.0.0.RELEASE
.
org.springframework:spring-context
您的 POM 中存在重复依赖项,可能导致类路径冲突。第一个是使用 version 的编译依赖项3.2.4.RELEASE
,第二个是使用 version 的运行时依赖项3.0.0.RELEASE
。
I suggest you remove the second since it's unnecessary and should be superseded by the first.
我建议你删除第二个,因为它是不必要的,应该被第一个取代。
回答by Remi Morin
Spring is not verbose about what is going wrong. To debug this, copy annotated class into the .war project and make it work there. A lot of thing may be wired wrong but spring will only say unsatisfied auto-wiring. It should work out-of-the-box, so something is not done properly. A property not set, a dependency not met etc. When you copy it in your project you simplify the problem, once it work there, move back the class and it should work fine!
Spring 并没有详细说明出了什么问题。要对此进行调试,请将带注释的类复制到 .war 项目中并使其在那里工作。很多东西可能接线错误,但弹簧只会说不满意的自动接线。它应该是开箱即用的,所以有些事情没有正确完成。未设置属性,未满足依赖项等。当您在项目中复制它时,您可以简化问题,一旦它在那里工作,将类移回,它应该可以正常工作!