【问题标题】:Using Spring Boot without the parent POM doesn't work在没有父 POM 的情况下使用 Spring Boot 不起作用
【发布时间】:2014-10-04 14:08:47
【问题描述】:

我正在尝试 SpringBoot QuickStart (http://projects.spring.io/spring-boot/#quick-start) 中的简单示例。 使用 spring-boot-starter-parent 作为父级工作正常。我可以毫无问题地运行示例。 但我绝对需要使用我自己的父母 pom。所以我阅读了文档: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-maven-without-a-parent

我试过了。它编译得很好。但是在运行时崩溃。 这是我的配置。 在我的父母 pom 中:

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

在我的 pom 中:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

但是当我运行应用程序时,我得到了这个:

2014-08-11 12:50:29.275 ERROR 28474 --- [ main] o.s.boot.SpringApplication : Application startup failed java.lang.IllegalStateException: Failed to introspect annotations: class com.imetrik.oaas.tool.application.springboottest.SampleController.SampleController at org.springframework.core.annotation.AnnotatedElementUtils.process(AnnotatedElementUtils.java:169) at org.springframework.core.annotation.AnnotatedElementUtils.isAnnotated(AnnotatedElementUtils.java:75) at org.springframework.core.type.StandardAnnotationMetadata.isAnnotated(StandardAnnotationMetadata.java:102) at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.registerBean(AnnotatedBeanDefinitionReader.java:138) at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.registerBean(AnnotatedBeanDefinitionReader.java:128) at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.register(AnnotatedBeanDefinitionReader.java:123) at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:159) at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:135) at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:127) at org.springframework.boot.SpringApplication.load(SpringApplication.java:620) at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) at org.springframework.boot.SpringApplication.run(SpringApplication.java:952) at org.springframework.boot.SpringApplication.run(SpringApplication.java:941) at com.imetrik.oaas.tool.application.springboottest.SampleController.SampleController.main(SampleController.java:25) 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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:673) at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:480) at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:306) at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:241) at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:88) at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:70) at java.lang.Class.initAnnotationsIfNecessary(Class.java:3178) at java.lang.Class.getAnnotations(Class.java:3158) at org.springframework.core.annotation.AnnotatedElementUtils.doProcess(AnnotatedElementUtils.java:195) at org.springframework.core.annotation.AnnotatedElementUtils.doProcess(AnnotatedElementUtils.java:212) at org.springframework.core.annotation.AnnotatedElementUtils.process(AnnotatedElementUtils.java:165) ... 18 common frames omitted Exception in thread "main" java.lang.IllegalStateException: Failed to introspect annotations: class com.imetrik.oaas.tool.application.springboottest.SampleController.SampleController at org.springframework.core.annotation.AnnotatedElementUtils.process(AnnotatedElementUtils.java:169) at org.springframework.core.annotation.AnnotatedElementUtils.isAnnotated(AnnotatedElementUtils.java:75) at org.springframework.core.type.StandardAnnotationMetadata.isAnnotated(StandardAnnotationMetadata.java:102) at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.registerBean(AnnotatedBeanDefinitionReader.java:138) at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.registerBean(AnnotatedBeanDefinitionReader.java:128) at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.register(AnnotatedBeanDefinitionReader.java:123) at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:159) at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:135) at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:127) at org.springframework.boot.SpringApplication.load(SpringApplication.java:620) at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) at org.springframework.boot.SpringApplication.run(SpringApplication.java:952) at org.springframework.boot.SpringApplication.run(SpringApplication.java:941) at com.imetrik.oaas.tool.application.springboottest.SampleController.SampleController.main(SampleController.java:25) 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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:673) at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:480) at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:306) at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:241) at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:88) at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:70) at java.lang.Class.initAnnotationsIfNecessary(Class.java:3178) at java.lang.Class.getAnnotations(Class.java:3158) at org.springframework.core.annotation.AnnotatedElementUtils.doProcess(AnnotatedElementUtils.java:195) at org.springframework.core.annotation.AnnotatedElementUtils.doProcess(AnnotatedElementUtils.java:212) at org.springframework.core.annotation.AnnotatedElementUtils.process(AnnotatedElementUtils.java:165) ... 18 more Process finished with exit code 1

我不知道如何解决这个问题。我尝试将 dependencyManagement 直接放在我的 pom 中(而不是在父级中)。结果相同。 我确实在 stackoverflow 和其他地方搜索过,但我找不到一个工作示例。 我们正在使用 Maven 3.0.4 和 Java 1.7U51

【问题讨论】:

  • 你需要查看有效的POM来检查最终的依赖关系。

标签: java spring spring-boot


【解决方案1】:

问题是我的父 pom 依赖于 Spring 3.2.5。 而且我发现 Spring-Boot 1.5.x 至少需要 Spring 4。 这在 Spring-Boot 页面上没有明确定义。

【讨论】:

  • 如果我没有在executions 标签中明确指出插件必须在repackage 阶段运行,这现在不起作用(至少对我而言,对于 Springboot 2.0.5)。
【解决方案2】:

我可能会迟到,但我相信这对每个想要运行 SpringBoot 而不从父级继承的人都会有所帮助。弄清楚以下内容非常令人沮丧,但它最终对我有用!

基本上,

  1. Intellij maven 自动导入已关闭。启用它。
  2. 我必须手动添加 spring-boot 依赖项。也就是说,如果你只是继承 spring-boot 作为父级,你将获得一组默认的依赖项来处理。但是当你不继承时,你必须在 pom.xml 中手动声明这些依赖项
  3. 如果你观察 pom.xml,“spring-boot-dependencies”必须在它自己的&lt;dependencyManagement&gt; 标签中。如果将所有依赖项组合在一个 &lt;dependencies&gt; 中,那么代码中的引用就会丢失!
  4. 请参见以下示例:

pom.xml

<?xml version="1.0" encoding="UTF-8"?> <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">
    <parent>
        <artifactId>Chethan</artifactId>
        <groupId>groupId</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>myspringbootwithoutparent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.4.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build> </project>

ApplicationWithoutParentPom.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by chethanshankar on 10/18/16.
 */
@RestController
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class ApplicationWithoutParentPom {
    @RequestMapping("/")
    String home() {
        return "Hello World without SPRING BOOT AS PARENT!!!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(ApplicationWithoutParentPom.class, args);
    }
}

【讨论】:

  • 必须在maven属性中指定spring的版本,没有它就不行。 &lt;spring.version&gt;a_version_of_spring&lt;/spring.version&gt;
猜你喜欢
  • 1970-01-01
  • 2020-04-24
  • 2020-12-23
  • 2018-11-18
  • 2015-08-03
  • 2017-11-04
  • 2016-09-20
  • 2018-07-12
  • 2017-10-09
相关资源
最近更新 更多