繁体   English   中英

如何创建多模块 Spring Boot Maven 项目?

[英]How to create Multimoduling Spring Boot Maven project?

我做了:

  • 创建了具有一些依赖项的项目。
  • 创建了第一个 Maven 模块(网络)。
  • 创建了第二个 Maven 模块(模型)。
  • 将代码移至 Web 模块:主类 + 控制器。
  • 从 root 中删除 src(因为它已经是空的)
  • 在模型模块创建的实体中,将依赖项 JPA/MySQl 驱动程序添加到该模块的 pom.xml。
  • 在 web 模块中添加了模型模块的依赖项。

项目运行没有错误, web模块看到模型依赖。 但是实体代码不起作用(不在数据库中创建表)。 如果我将课程转移到网络模块 - 它会起作用。 没错 - 它可以工作,但 JPA/MySQL 的依赖项仍在模型模块中。

我在做什么错?

我尝试使用包的模块名称更改 groupId 和 artifactId 的名称,将 application.propeties 移动到模型模块 - 无济于事。

我的项目:

-模型/src/main/java 和 resources/com/example/demo/model

pom.xml:

<modelVersion>4.0.0</modelVersion>

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

    <groupId>com.example.demo.model</groupId>
    <artifactId>model</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>module-model</name>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

- web /src/main/java 和 resources/com/example/demo/web

application.properties 与 MySQL 的数据库连接。

pom.xml

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

    <groupId>com.example.demo.web</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>module-web</name>
    <description>Web project</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>com.example.demo.model</groupId>
            <artifactId>model</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

主要 pom.xml:

<modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.1.0</version>
    <packaging>pom</packaging>

    <description>Demo project for Spring Boot</description>
    <name>demo</name>

    <modules>
        <module>web</module>
        <module>model</module>
    </modules>

Anrew 您还可以使用@ComponentScan 使用“scanBasePackages”来调整@SpringBootApplication(scanBasePackages = { "com.example.demo.model", "com.example.demo.web" }) 注释。 请记住,您必须在“Web 项目”中添加此注释

@SpringBootApplication(scanBasePackages = "com.example.demo.model","com.example.demo.web" })
public class WebApplication {

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

}

@ComponentScan 告诉 Spring 您在哪些包中注释了应该由 Spring 管理的类。 默认情况下,它会扫描主包,因此如果 com 下的 spring 启动应用程序。 示例只会扫描此包下的所有类。 因此,如果您将它们移动到不同的包下,Spring 应用程序将无法识别它们。

如果是这种情况,您可以添加多个要扫描的包 @ComponentScan({ "xyz", "xyzdao" })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM