为什么Maven构建不能解析Spring @Bean @Configuration等注释?

时间:2013-08-31 14:35:44

标签: java spring maven spring-mvc spring-annotations

我正在尝试将我的Spring MVC项目移至3.2.4。

当我尝试在IntelliJ中运行应用程序时,如前所述使用Maven,我收到的错误是无法从Spring中找到一系列注释类...

@Bean
@Configuration
@ComponentScan
@PropertySource

都无法解决类错误消息而失败。

其他注释没问题,这让我觉得这是一个依赖问题,因为Spring版本是我项目代码库的主要变化。

Spring 3.2.4是否更改了包装中的注释?

我当前的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>

2 个答案:

答案 0 :(得分:3)

POM中存在org.springframework:spring-context的重复依赖项,可能导致类路径冲突。第一个是使用版本3.2.4.RELEASE的编译依赖项,第二个是使用版本3.0.0.RELEASE的运行时依赖项。

我建议你删除第二个,因为它是不必要的,应该被第一个取代。

答案 1 :(得分:0)

对于出了什么问题,Spring并不啰嗦。要对此进行调试,请将带注释的类复制到.war项目中并使其在那里工作。很多事情可能是错误的,但春天只会说不满意的自动接线。 它应该是开箱即用的,所以有些事情没有做好。未设置的属性,未满足的依赖性等。 当你在项目中复制它时,你可以简化问题,一旦它在那里工作,移回课程它应该工作正常!