Spring Boot (kotlin) 循环依赖

Posted

技术标签:

【中文标题】Spring Boot (kotlin) 循环依赖【英文标题】:Spring Boot (kotlin) circular dependency 【发布时间】:2020-07-15 05:37:33 【问题描述】:

我使用 Spring Boot Initializer 创建了 Spring Boot 项目。我添加了核心、数据和服务模块。现在我在构建后和 IntelliJ 项目结构中看到一个警告:项目 exampleapi:“服务”模块、“数据”模块、“核心”模块之间存在循环依赖关系。

这是我的 pom:

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<modules>
    <module>service</module>
    <module>data</module>
    <module>core</module>
</modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.mjana</groupId>
    <artifactId>exampleapi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>exampleapi</name>
    <description>Example Api</description>

    <properties>
        <java.version>11</java.version>
        <kotlin.version>1.3.71</kotlin.version>
    </properties>

    <repositories>
        <repository>
            <id>exposed</id>
            <name>exposed</name>
            <url>https://dl.bintray.com/kotlin/exposed</url>
        </repository>
    </repositories>

    <dependencies>
        <!--Modules-->
        <dependency>
            <groupId>com.mjana</groupId>
            <artifactId>core</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.mjana</groupId>
            <artifactId>service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.mjana</groupId>
            <artifactId>data</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!--Data-->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>8.2.1.jre11</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.exposed</groupId>
            <artifactId>exposed-spring-boot-starter</artifactId>
            <version>0.22.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-kotlin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>$project.basedir/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>$project.basedir/src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                    </compilerPlugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>$kotlin.version</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

并且每个模块的pom都是一样的,除了artifactId:

    <?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>exampleapi</artifactId>
        <groupId>com.mjana</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service</artifactId>
    <packaging>jar</packaging>
</project>

【问题讨论】:

您遇到的问题或具体问题是什么? 【参考方案1】:

您会收到循环依赖错误 b/c 每个子模块都继承了父配置,您在其中声明了对子模块的依赖关系。因此循环依赖错误。

您需要从每个子级中删除 parent 引用,以免它们继承此依赖项配置。

【讨论】:

以上是关于Spring Boot (kotlin) 循环依赖的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot源码:循环依赖

为啥我在 Spring-boot 应用程序中通过 SessionFactory 有循环依赖?

在 pom.xml 中为 Kotlin 中的 Spring Boot 项目指定 Main 类

Spring Boot 2.6 + 集成 - internalPublisherAnnotationBeanPostProcessor 循环依赖

Spring Boot 2.0.0 循环bean依赖EntityManger和SessionFactory

使用 Spring Boot 的 RESTful API 中的循环依赖