在没有父 POM 的情况下使用 Spring Boot 不起作用

tle*_*que 2 java spring spring-boot

我正在尝试 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>
Run Code Online (Sandbox Code Playgroud)

在我的pom中:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

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


    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

Che*_*han 6

我可能会迟到,但我相信这对想要在不继承 parent 的情况下运行SpringBoot 的每个人都有帮助。计算以下内容非常令人沮丧,但最终它对我有用!

基本上,

  1. Intellij maven 自动导入已关闭。启用它。
  2. 我必须手动添加 spring-boot 依赖项。即如果你只是继承 spring-boot 作为父级,你将获得一组默认的依赖项来处理。但是当您不继承时,您必须在 pom.xml 中手动声明这些依赖项
  3. 如果你观察 pom.xml,“spring-boot-dependencies”必须在它自己的<dependencyManagement>标签中。如果将所有依赖项组合在一个 中<dependencies>,则代码中的引用将丢失!
  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>
Run Code Online (Sandbox Code Playgroud)

应用程序没有ParentPom.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);
    }
}
Run Code Online (Sandbox Code Playgroud)


tle*_*que 1

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