Spring Boot(kotlin)循环依赖 [英] Spring Boot (kotlin) circular dependency

查看:101
本文介绍了Spring Boot(kotlin)循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Spring Boot Initializer创建了Spring Boot项目.我添加了核心,数据和服务模块.现在,我在构建后以及IntelliJ项目结构中看到警告:Project exampleapi:服务"模块,数据"模块,核心"模块之间存在循环依赖关系.

I created the Spring Boot project with the Spring Boot Initializer. I have added Core, Data and Service modules. Now I see a warning after the build and in IntelliJ Project Structure: Project exampleapi: there is circular dependency between 'service' module, 'data' module, 'core' module.

这是我的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:

And the pom of each module is the same except for 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>

推荐答案

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

You get circular dependency error b/c each of the child modules inherits the parent's configuration where you declare dependencies on the childs. Hence the circular dependency error.

您需要从每个子级中删除parent引用,以使他们不要继承此依赖项配置.

You need to remove parent reference from each of the childs for them not to inherit this dependency configuration.

这篇关于Spring Boot(kotlin)循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆