首页 文章

具有自定义父级的Spring Boot App模块 - 使其正常工作

提问于
浏览
1

目标:让Spring Boot应用程序模块与spring-boot-starter-parent完美配合,使用我们自己的自定义父pom . 我正在遵循这个指南,很遗憾地说我无法完全开始工作 . https://www.surasint.com/spring-boot-with-no-parent-example/ .

具体地说,将spring-boot-starter作为父pom一切正常 .

使用我们自己的父pom,Spring Boot应用程序会中断 .

我有一个项目设置有三个模块和我自己的父pom . 出于多种原因需要这样做(父pom使用Maven配置文件并根据这些配置文件设置重要的数据库变量) .

父pom(我们的定制) .

  • Common - 简单的jar库 .

  • Web - 具有REST服务和纯Java帮助程序的Spring Boot应用程序(不是任何jsp或html) . 这曾经从spring-boot-starter父继承 - 现在必须从我们的自定义父pom继承 .

  • Worker - 带有工作线程的简单jar库 .

我使用的是Spring Boot 1.5.9,Maven 3.3和Java 8 .
从理论上讲,我完全同意它应该在没有spring-boot-starter-parent的情况下工作 . 但是,当使Spring Boot模块仅从我们的父pom继承而不是从spring-boot-starter-parent继承时,会发生以下(对我非常奇怪)异常 .

2018-02-07 16:06:46.822 ERROR 10008 --- [           main] 
    o.s.boot.SpringApplication               : Application startup failed                  

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcValidator' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.validation.Validator]: Factory method 'mvcValidator' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration.getApplicationContext()Lorg/springframework/context/ApplicationContext; 

        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 

        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]

该项目在IntelliJ中是完全绿色的,编译就可以了 . 只有在启动Spring Boot应用程序时才会出现这些错误 . 正如我所解释的那样,Spring启动应用程序模块现在缺少它之前的功能 . 这里的任何建议都会有所帮助!

具体来说,你需要导入什么(在Maven依赖项标签中)才能让模块能够访问完全相同的东西 - 以及它从spring-boot-starter-parent继承而不是你自己的东西 . 定制父母?它是超级的,而不是纯粹的直觉猜测你的依赖(在我的情况下显然是失败的)有一个参考依赖检查列表,确保你的模块可以访问完全相同的东西,就像从spring-boot-starter-继承一样父母直接 .

到目前为止,我通过纯粹的直觉将以下内容添加到Spring引导模块(上面称为“web”)

<!-- === [Spring Boot Starter] === --> 
<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter</artifactId> 
</dependency> 
<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-test</artifactId> 
  <scope>test</scope> 
</dependency> 
<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-web</artifactId> 
</dependency>

显然缺少一些东西,就像spring-boot-starter作为父母一样 . 而且 - 奇怪的是 - 剩下的可能的spring-boot-starter依赖项看起来与项目完全无关(像 Cloud 连接器,twitter-connectors,mongodb和相关的东西) . 我添加了所有远程相关的测试,但下面的错误仍然存在 . 任何建议或帮助都非常感谢 .

作为参考,这里是父pom:

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd“> 4.0.0 no.statnett.fasit fasit-hub-backend 1.0-SNAPSHOT普通工作者网站pom Fasit Hub后端

<!-- parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent -->

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.source.encoding>UTF-8</project.source.encoding>
    <project.source.version>1.8</project.source.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

    <spring.boot.version>1.5.9.RELEASE</spring.boot.version>
    <failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

<dependencyManagement>
    <dependencies>
        <!--
            Import dependency management from Spring Boot
            * This fixes the Spring boot parent issue [inherit dependencyManagement without forcing Spring Boot App]
            * It does NOT include the plugin management
            See https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-maven-without-a-parent
            See http://www.logicbig.com/tutorials/spring-framework/spring-boot/starter-import/
         -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring.boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring.boot.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <!-- === [Spring Boot JPA] ===-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.196</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0.1</version>
        </dependency>
        <!-- === [Spring Boot Security] ===-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <!-- === [Spring Boot Maven plugin needed for profile management] ===-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <!-- === [Messaging] ===-->
        <!--
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-broker</artifactId>
        </dependency>
        -->
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>1.7.2.RELEASE</version>
        </dependency>

        <!-- ===[Messaging with AMQP over JMS with the Apache QPID client] === -->
        <dependency>
            <groupId>org.apache.qpid</groupId>
            <artifactId>qpid-jms-client</artifactId>
            <version>0.24.0</version>
        </dependency>
        <!-- === [Date and Time management] ===-->
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.9.4</version>
        </dependency>

        <!-- === [XStream for converting classes to and from XML] === -->
        <!-- NB!
        XStream also needs Apache's commons-io
        2017.10.15: The artifact org.apache.commons:commons-io:jar:1.3.2 has been relocated to commons-io:commons-io:jar:1.3.2
        See https://mvnrepository.com/artifact/org.apache.commons/commons-io
        See https://mvnrepository.com/artifact/commons-io/commons-io
        -->
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.10</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!-- === [Spring MVC and WEB (javax.servlet, JSP/JSTL] === -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- === [Logging and Testing] === -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

    </dependencies>
</dependencyManagement>

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <activatedProperties>dev</activatedProperties>
            <datasourceURL>jdbc:oracle:thin:@h1-a-oradb10t:1521:fasitngft1</datasourceURL>
            <datasourceUser>fasithub</datasourceUser>
            <datasourcePassword>cH9nIiBzuS5Ho3q1bOP5</datasourcePassword>
            <datasourceDriver>oracle.jdbc.OracleDriver</datasourceDriver>
            <ecpServer>amqp://ec2-52-34-81-131.us-west-2.compute.amazonaws.com:5672</ecpServer>
            <ecpServerSolnett>amqp://ec2-52-40-134-91.us-west-2.compute.amazonaws.com:5672</ecpServerSolnett>
            <ecpServerVindnett>amqp://ec2-34-213-80-66.us-west-2.compute.amazonaws.com:5672</ecpServerVindnett>
            <ecpEndpointSuffix></ecpEndpointSuffix>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <activatedProperties>test</activatedProperties>
            <datasourceURL>jdbc:h2:file:~/test</datasourceURL>
            <datasourceUser>sa</datasourceUser>
            <datasourcePassword></datasourcePassword>
            <datasourceDriver>org.h2.Driver</datasourceDriver>
            <ecpServer>amqp://ec2-52-34-81-131.us-west-2.compute.amazonaws.com:5672</ecpServer>
            <ecpServerSolnett>amqp://ec2-52-40-134-91.us-west-2.compute.amazonaws.com:5672</ecpServerSolnett>
            <ecpServerVindnett>amqp://ec2-34-213-80-66.us-west-2.compute.amazonaws.com:5672</ecpServerVindnett>
            <ecpEndpointSuffix></ecpEndpointSuffix>
        </properties>
    </profile>
    <profile>
        <id>staging</id>
        <properties>
            <activatedProperties>staging</activatedProperties>
            <datasourceURL>jdbc:oracle:thin:@h1-a-oradb10t:1521:fasitngft1</datasourceURL>
            <datasourceUser>fasithub</datasourceUser>
            <datasourcePassword>cH9nIiBzuS5Ho3q1bOP5</datasourcePassword>
            <datasourceDriver>oracle.jdbc.OracleDriver</datasourceDriver>
            <ecpServer>amqp://h1-a-ecp4end:6672</ecpServer>
            <ecpServerSolnett>amqp://ec2-52-40-134-91.us-west-2.compute.amazonaws.com:5672</ecpServerSolnett>
            <ecpServerVindnett>amqp://ec2-34-213-80-66.us-west-2.compute.amazonaws.com:5672</ecpServerVindnett>
            <ecpEndpointSuffix>FASIT-SERVICE</ecpEndpointSuffix>
        </properties>
    </profile>
</profiles>

这里是 spring 启动应用程序模块pom(这里是当前的反复试验发生者):

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd“> 4.0.0

<!-- Alt.1: have our own parent: this is the way to go for a common jar module.
    ISSUE: Spring Boot module fails to run - applicationContext fails to load!
    Solution: explicitly define goal for spring-boot-maven-plugin (If you use Spring Boot as parent, you do not have to explicitly define this)
-->
<parent>
    <groupId>no.statnett.fasit</groupId>
    <artifactId>fasit-hub-backend</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<!-- Alt.2: have spring-boot-starter-parent as parent. This is useful for a Spring Boot app module.
    ISSUE: profile management breaks - properties are not set on the Spring Boot module!
    Solution: not possible without our own parent pom
-->
<!-- parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent -->

<!-- Spring Boot starter properties overrule (default Java is only 1.6) -->
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <!-- spring.boot.version>1.5.6.RELEASE</spring.boot.version -->
</properties>

<groupId>no.statnett.fasit</groupId>
<artifactId>web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<!-- === [Dependency Management] === -->
<dependencies>
    <dependency>
        <groupId>no.statnett.fasit</groupId>
        <artifactId>common</artifactId>
        <version>${project.version}</version>
    </dependency>

    <!-- === [Spring Boot Starter] === -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <!-- Following three dependencies where not needed with spring-boot-starter-parent as parent:
     spring-boot-starter-tomcat, spring-boot-starter-thymeleaf, spring-boot-starter-data-rest
     STILL GET ISSUE:
     Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate
        [org.springframework.data.rest.core.config.RepositoryRestConfiguration]:
        Factory method 'config' threw exception; nested exception is java.lang.NoClassDefFoundError:
        org/springframework/data/rest/core/config/RepositoryCorsRegistry
    -->

    <!-- === [Spring Boot JPA] === -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.javassist</groupId>
                <artifactId>javassist</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- === [Oracle driver official, manually installed] ===-->
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.3.0</version>
        <type>pom</type>
    </dependency>

    <!-- === [Spring Boot Security] ===-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <!-- === [Date and Time management] ===-->
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.3</version>
    </dependency>

    <!-- === [XStream for converting classes to and from XML] === -->
    <!-- NB! moved to the common module, they are not needed here in the web service module! -->
</dependencies>

<!-- === [Build management for the Spring Boot module] === -->
<build>
    <plugins>
        <!-- === [Spring Boot Maven plugin is used for profile management] ===-->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring.boot.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- === [Apache Maven compiler plugin for building] ===-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

UPADTE 1: 如果我将以下三个依赖项添加到spring app模块pom:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

我反而得到以下的例外情况:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.rest.core.config.RepositoryRestConfiguration]: Factory method 'config' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/data/rest/core/config/RepositoryCorsRegistry
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
... 144 common frames omitted

引起:java.lang.NoClassDefFoundError:org / springframework / data / rest / core / config / RepositoryCorsRegistry

我不知道如何解释这一点 . 这是否意味着我们走在正确的轨道上,只需要添加一些额外的依赖性,或者在不必要地添加依赖项并且可能导致不可靠的问题的情况下?

缺少的类现在是https://docs.spring.io/spring-data/rest/docs/current/api/org/springframework/data/rest/core/config/RepositoryCorsRegistry.html我不确定要添加什么作为依赖项以使用Spring Boot以正确的方式访问此类 . 当然,我不愿意通过像_2532306这样的普通 spring 框架依赖来添加它,因为这通常会导致版本问题(Spring Boot包含相关的springframeworkd,应该完全通过Spring Boot来管理,因为我理解它并被多次告知) .

UPDATE 2: 或许反对更好的判断 - 因为我愿意测试任何事情,直到事情正常 - 我添加了以下具有当前缺失类的库 .

这当然只有在仔细检查https://mvnrepository.com/artifact/org.springframework.boot/spring-boot/1.5.9.RELEASE以确定Spring Boot版本使用哪个核心org.springframework库之后才能完成 .

<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-core</artifactId> 
<version>4.3.13.RELEASE</version> 
</dependency> 
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-context</artifactId> 
<version>4.3.13.RELEASE</version> 
</dependency> 
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-web</artifactId> 
<version>4.3.13.RELEASE</version> 
</dependency>

我现在得到一个不同的例外:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.filter.OrderedHttpPutFormContentFilter]: Factory method 'httpPutFormContentFilter' threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind.ObjectMapper 

    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 

    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 

    ... 26 common frames omitted 

Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind.ObjectMapper 

    at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.build(Jackson2ObjectMapperBuilder.java:588) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]

这导致我添加以下依赖项(再次仔细检查上面的Spring Boot参考,看看它使用的特定库的版本):

<dependency> 
<groupId>com.fasterxml.jackson.core</groupId> 
<artifactId>jackson-databind</artifactId> 
<version>2.9.4</version> 
</dependency>

然而,遗憾的是奇怪的是 - 同样的例外仍然存在 . 这意味着我们已经走到了死胡同,因为https://mvnrepository.com/artifact/org.springframework.boot/spring-boot/1.5.9.RELEASE不再需要包含逻辑库 .

无论如何,在使用Spring Boot应用程序时,最好不要包含特定的核心springframework库版本 .

让我们退后一步,看看我们需要什么 .

  • Spring Boot应用程序模块包含一组REST服务 .

  • 一组服务只是GET请求,并产生application / xml(用于与alpha类型的系统集成)

  • 另一组服务生成并使用application / json(用于与类型为omega的系统集成) .

而已 . 所有核心服务和JPA逻辑都位于Web Spring Boot应用程序模块和worker模块所需的公共库模块中 .

结论:我们需要导入哪些spring-boot依赖项才能使其正常工作?将spring-boot-starter-parent作为pom parent时,它的工作完全正常 - 但在给定的情况下这是不可能的 . 我只想说终于让这个工作变得超级好

干杯!

2 回答

  • 0

    我现在终于通过强力试错法测试解决了这个问题 . 可以说,有大量可能的依赖项组合进行测试 . 作为开发人员,这是您可以执行的最繁琐和浪费时间的任务 . 我并不认为这只是一个可接受的答案,因为我对此并不满意 .

    可以说,它实际上是超级的依赖包含的引用集,所以你得到_t32313_你得到的spring-boot-starter-parent . 在使用Spring Boot时,最好不要包含特定的核心org.springframework依赖项 . 如果您没有超级自律,并仔细检查您的特定Spring Boot版本使用的确切库版本,您将遇到问题 .

    最佳实践 - 据我所知并且多次被告知 - 是您只导入纯Spring Boot依赖项,Spring Boot管理其余的 . 显然,令人遗憾和令人惊讶的是,情况并非如此 . 如果有人有相反的证据,我都是耳朵 .

    总之,这些是解决包含REST服务的spring boot app模块的依赖项(包括application / json和application / xml以及自动对象映射) .

    <!-- === [Spring Boot Starter] === --> 
    <!-- Compile --> 
    <dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter</artifactId> 
    </dependency> 
    <dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-actuator</artifactId> 
    </dependency> 
    <dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <!-- NB! 
    spring-boot-starter-web also needs the following [Spring Boot app fails to start without them] 
    These dependencies where NOT needed with spring-boot-starter-parent as parent. 
    Without them you e.g. get the following on Spring Boot app start: 
    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate 
    [org.springframework.data.rest.core.config.RepositoryRestConfiguration]: 
    Factory method 'config' threw exception; nested exception is java.lang.NoClassDefFoundError: 
    org/springframework/data/rest/core/config/RepositoryCorsRegistry 
    --> 
    <dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-databind</artifactId> 
    <version>2.8.10</version> 
    </dependency> 
    <dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-validator</artifactId> 
    <version>5.3.6.Final</version> 
    </dependency> 
    <dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-web</artifactId> 
    <version>4.3.13.RELEASE</version> 
    </dependency> 
    <dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-webmvc</artifactId> 
    <version>4.3.13.RELEASE</version> 
    </dependency> 
    <!-- Provided --> 
    <dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-tomcat</artifactId> 
    <scope>provided</scope> 
    </dependency> 
    <!-- Test --> 
    <dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-test</artifactId> 
    <scope>test</scope> 
    </dependency>
    
  • 0

    是否有一些原因导致Spring Boot Starter Parent不能成为您父pom的父级?我看到它在您的自定义父级中被注释掉了 .

    此设置应该有效:

    Custom Parent pom

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.16.RELEASE</version>
    </parent>
    
    <!-- All of your customizations follow -->
    <dependencies>
        <!-- Define common dependencies for all child modules-->
    </dependencies>
    
    <dependencyManagement>
        <!-- Define optional module dependencies with preferred versions -->
        <!-- This can help the module developers to not have to deal with dependency conflicts if you handle them here -->
    </dependencyManagement>
    
    <build>
        <plugins>
            <!-- Define common plugin configs for all child modules -->
        </plugins>
    
        <pluginManagement>
            <plugins>
                <!-- Define optional plugin configs for child modules -->
            </plugins>
        </pluginManagement>
    </build>
    

    App pom

    <parent>
        <groupId>no.statnett.fasit</groupId>
        <artifactId>fasit-hub-backend</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    

    老实说,应该是这样 . 然后,您已经创建了一个从Spring Boot Starter Parent正确继承所有内容的自定义父pom,您可以提供您希望App模块继承的所有自己的配置文件和其他自定义 .

    您不必经历重新创建Spring Boot应用程序的依赖关系树的所有麻烦,正如您所指出的那样,这样做基本上是依赖关系管理的一个难题 .

相关问题