Search code examples
spring-bootrepositorypom.xmlmulti-moduleapplication.properties

Is it possible for a module of a multi module project to use the repository folder of another?


I was following a tutorial to learn multi-modules in maven, but what was being presented raised a question:

Is there a possibility to create a multi-module project where only one module accesses the database and the others use this connection for their respective controllers?

Basically what I want is to use the same repository folder to scan with each datasourceconfig in all modules.

The structure I imagined and even started to implement was:

|--main
|  |--moduleOne
|  |--|--Java
|  |-----|-DataSource
|  |-----|-models
|  |-----|-repositories
|  |--|--Resources
|  |-----|-application.properties
|  |--moduleN
|  |--|--Java
|  |-----|-controller
|  |-----|-service
|  |--|--Resources
|  |-----|-application.properties

The first module run perfectyly, the other need a conetion and I did It, but when I run, this module use a h2 database or not connect because moduleOne is using repository folder. How can I fix for that services access those repositories?

POM ModuleOne

<project>
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <groupId>com.example</groupId>
        <artifactId>sig</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../</relativePath> <!-- lookup parent from repository -->
    </parent>

    <artifactId>moduleOne</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>moduleOne</name>

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

    <dependencies>
        <!-- SQL DEPENDENCIES -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>moduleOne</finalName>
    </build>
</project>

POM ModuleN

<project>
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <groupId>com.example</groupId>
        <artifactId>sig</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../</relativePath> <!-- lookup parent from repository -->
    </parent>

    <artifactId>moduleN</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>moduleN</name>

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

    <dependencies>
        <!-- MODULE ON WHICH IT DEPENDS -->
        <dependency>
            <groupId>com.example.sig</groupId>
            <artifactId>moduleOne</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>moduleN</finalName>
    </build>
</project>

Solution

  • "The bean 'RepositoryX', defined in br.example.sig.moduleone.repositories.RepositoryX defined in EnableJpaRepositories declared on ServiceX, could not be registered. A bean with that name has already been defined in br.example.sig.moduleone.repositories.RepositoryX defined in EnableJpaRepositories declared on DatasourceConfig and overriding is disabled.

    Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true"

    I had a service with the annotations "@EntityScan" and "@EnableJpaRepositories" pointing to the same directory as my datasource in one of the modules, I put it there and forgot it. I deleted the annotations, that was what generated the conflict and not the use for different modules.