Spring-Boot-Maven-Plugin,无法实例化接口org.springframework.context.ApplicationContextInitializer

时间:2018-06-18 15:33:47

标签: java maven spring-mvc spring-boot

我正在使用自定义spring mvc父项目开发一个spring boot maven项目。我无法让mvn spring-boot:run工作。这是我的POM.xml忽略父标签和groupId,artifactID等... tags`

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <netbeans.hint.deploy.server>Tomcat</netbeans.hint.deploy.server>
    <webtarget.root>${basedir}/target/</webtarget.root>
    <webtarget.base>${basedir}/target/tsm-webapp</webtarget.base>
</properties>

<repositories>
    <repository>
        <id>Nexus</id>
        <name>Nexus Managed Internal Repository</name>
        <url>http://nexus.itg.ti.com/nexus/content/groups/public</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20090211</version>
    </dependency>            

    <!-- === web assets === -->

    <!-- jquery -->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>2.1.3</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery-cookie</artifactId>
        <version>1.4.1-1</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery-validation</artifactId>
        <version>1.14.0-1</version>
    </dependency>

    <!-- font-awesome -->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>font-awesome</artifactId>
        <version>4.6.3</version>
    </dependency>

    <!-- datatables -->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>datatables</artifactId>
        <version>1.10.11</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>datatables-plugins</artifactId>
        <version>1.10.11-1</version>
    </dependency>
    <!--<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <type>jar</type>
    </dependency> -->

    <!-- momentjs -->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>momentjs</artifactId>
        <version>2.11.1</version>
    </dependency>

    <!--ums-->
    <!--new ui changes-->
    <dependency>
        <groupId>com.ti.spring.service</groupId>
        <artifactId>usersvc-ui</artifactId>
        <version>TSM-1.0-27-RELEASE</version>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>tsm-engine</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>servlet-api</artifactId>
        <version>3.0.20100224</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.2</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.0.3.RELEASE</version>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <finalName>tsm-checker</finalName>
    <plugins>

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.0.3.RELEASE</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>


        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <packagingExcludes>WEB-INF/web.xml</packagingExcludes>
                <attachClasses>true</attachClasses>
                <classesClassifier>classes</classesClassifier>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
            <path>/</path>
            </configuration>
        </plugin>
        <!-- Copies external config files from project to output directory -->
        <!--<plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
            </executions>
        </plugin>-->

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.target}</target>
            </configuration>
        </plugin>

    </plugins>
</build>

` 我知道,一旦我在依赖关系管理中添加了spring boot依赖项,插件就不再自动配置了。我想知道我的spring boot maven插件配置是否有问题,或者是否有其他原因导致问题。

这是我的完整堆栈跟踪

    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:496)
    at java.lang.Thread.run (Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationContextInitializer : org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances (SpringApplication.java:448)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances (SpringApplication.java:427)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances (SpringApplication.java:418)
    at org.springframework.boot.SpringApplication.<init> (SpringApplication.java:266)
    at org.springframework.boot.SpringApplication.<init> (SpringApplication.java:247)
    at org.springframework.boot.SpringApplication.run (SpringApplication.java:1255)
    at org.springframework.boot.SpringApplication.run (SpringApplication.java:1243)
    at com.ti.tmg.tsm.checker.Application.main (Application.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:496)
    at java.lang.Thread.run (Thread.java:748)
Caused by: java.lang.NoClassDefFoundError: org/springframework/core/KotlinDetector
    at org.springframework.beans.BeanUtils.instantiateClass (BeanUtils.java:169)
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances (SpringApplication.java:444)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances (SpringApplication.java:427)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances (SpringApplication.java:418)
    at org.springframework.boot.SpringApplication.<init> (SpringApplication.java:266)
    at org.springframework.boot.SpringApplication.<init> (SpringApplication.java:247)
    at org.springframework.boot.SpringApplication.run (SpringApplication.java:1255)
    at org.springframework.boot.SpringApplication.run (SpringApplication.java:1243)
    at com.ti.tmg.tsm.checker.Application.main (Application.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:496)
    at java.lang.Thread.run (Thread.java:748)
Caused by: java.lang.ClassNotFoundException: org.springframework.core.KotlinDetector
    at java.net.URLClassLoader.findClass (URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass (ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass (ClassLoader.java:357)
    at org.springframework.beans.BeanUtils.instantiateClass (BeanUtils.java:169)
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances (SpringApplication.java:444)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances (SpringApplication.java:427)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances (SpringApplication.java:418)
    at org.springframework.boot.SpringApplication.<init> (SpringApplication.java:266)
    at org.springframework.boot.SpringApplication.<init> (SpringApplication.java:247)
    at org.springframework.boot.SpringApplication.run (SpringApplication.java:1255)
    at org.springframework.boot.SpringApplication.run (SpringApplication.java:1243)
    at com.ti.tmg.tsm.checker.Application.main (Application.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:496)
    at java.lang.Thread.run (Thread.java:748)

1 个答案:

答案 0 :(得分:1)

看看

的结果
mvn dependency:tree

包含父项目,看看您是否有多个版本的org.springframework:spring-core

您正在此基本pom中加载正确的spring-core版本(5.0.7.RELEASE)。但是如果你的父MVC项目正在加载一个不同的(较旧的)Spring版本,你可能会得到那个缺少堆栈跟踪中引用的类的旧版本(KotlinDetector)。