简体   繁体   中英

Spring dependency injection in maven multi modules project

I'm building a multi modules maven project with 4 modules. For two of them (the one with the rest controller and the one with the core business logic) I need the power of the dependency injection. But is not working at all. Here my parent pom:

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

....

<modules>
        <module>taskexecutor-service</module>
        <module>taskexecutor-core</module>
        <module>taskexecutor-common</module>
        <module>taskexecutor-rules</module>
</modules>

.....

Child pom service:

<parent>
    <groupId>com.example.taskexecutor</groupId>
    <artifactId>taskexecutor</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>taskexecutor-service</artifactId>

.....

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </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>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.example.taskexecutor</groupId>
            <artifactId>taskexecutor-core</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

Child pom core (the one with the class that i can't inject into the service project)

....
<parent>
    <groupId>com.example.taskexecutor</groupId>
    <artifactId>taskexecutor</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>taskexecutor-core</artifactId>

...

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
</dependency>

The package inside the two project (service and core) are the same: com.example.application (where is the main class with @SpringBootApplication in the service project), com.example.application.restcontroller (where is the controller in the service project), com.example.application.core (where is the @component that I can't inject in the core project).

Inside the core project I have, under the com.example.application.core a @Component that I would inject inside the class into the service project under the com.example.application.restcontroller package. Within the same project and under the package of the main class, the dependency injection works absolutely fine. Between two different modules I can't achieve that (nullPointerException).

Any advice will be appreciated

Your description about the packages is hard to understand. But, it most likely is a Component scanning issue. Make sure that you are scanning all the packages. For instance, In the taskexecutor-service , you can do: @ComponentScan(basePackages = {"com.example.taskexecutor", "com.example.application"})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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