简体   繁体   English

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

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

I did:我做了:

  • created project with some dependencies.创建了具有一些依赖项的项目。
  • created first maven module(web).创建了第一个 Maven 模块(网络)。
  • created second maven module(model).创建了第二个 Maven 模块(模型)。
  • moved code to web module: main class + controller.将代码移至 Web 模块:主类 + 控制器。
  • deleted src from root(because it is already empty)从 root 中删除 src(因为它已经是空的)
  • in model module created entity, added dependencies JPA/MySQl driver to pom.xml of this module.在模型模块创建的实体中,将依赖项 JPA/MySQl 驱动程序添加到该模块的 pom.xml。
  • in web module added dependency of model module.在 web 模块中添加了模型模块的依赖项。

Project runs without errors, web module sees model dependency.项目运行没有错误, web模块看到模型依赖。 But code of entity doesnt work(not creating table in database).但是实体代码不起作用(不在数据库中创建表)。 If i move class to web module - it works.如果我将课程转移到网络模块 - 它会起作用。 Exactly - it works, but dependencies of JPA/MySQL are still in model module.没错 - 它可以工作,但 JPA/MySQL 的依赖项仍在模型模块中。

What i am doing wrong?我在做什么错?

I tried to change names of groupId and artifactId, with package`s names of modules, moved application.propeties to model module - does not help.我尝试使用包的模块名称更改 groupId 和 artifactId 的名称,将 application.propeties 移动到模型模块 - 无济于事。

My project:我的项目:

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

pom.xml: 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 and resources/com/example/demo/web - web /src/main/java 和 resources/com/example/demo/web

application.properties with database connection to MySQL. application.properties 与 MySQL 的数据库连接。

pom.xml 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>

Main pom.xml:主要 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 you also may adjust @SpringBootApplication(scanBasePackages = { "com.example.demo.model", "com.example.demo.web" }) annotation with "scanBasePackages" it uses @ComponentScan. Anrew 您还可以使用@ComponentScan 使用“scanBasePackages”来调整@SpringBootApplication(scanBasePackages = { "com.example.demo.model", "com.example.demo.web" }) 注释。 And bear in mind that you have to add this annotation in "Web project"请记住,您必须在“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 tells Spring in which packages you have annotated classes which should be managed by Spring. @ComponentScan 告诉 Spring 您在哪些包中注释了应该由 Spring 管理的类。 and by default, it scans the main package so if spring boot application under com.默认情况下,它会扫描主包,因此如果 com 下的 spring 启动应用程序。 example only all Classes under this package will be scanned.示例只会扫描此包下的所有类。 so if you moved them under different package Spring application will not recognize them.因此,如果您将它们移动到不同的包下,Spring 应用程序将无法识别它们。

if this is the case you can add multiple packages to be scanned @ComponentScan({ "xyz", "xyzdao" })如果是这种情况,您可以添加多个要扫描的包 @ComponentScan({ "xyz", "xyzdao" })

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

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