[英]Spring dependency injection in maven multi modules project
I'm building a multi modules maven project with 4 modules.我正在构建一个包含 4 个模块的多模块 Maven 项目。 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:
这是我的父母 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:儿童 pom 服务:
<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)子pom核心(我不能注入到服务项目中的那个类)
....
<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).两个项目(service和core)里面的包是一样的:com.example.application(服务项目中带有@SpringBootApplication的主类),com.example.application.restcontroller(服务中的控制器在哪里项目),com.example.application.core(我无法在核心项目中注入的@component 在哪里)。
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.在我的核心项目中,在 com.example.application.core 下有一个 @Component,我会将它注入到类中的 com.example.application.restcontroller 包下的服务项目中。 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).
在两个不同的模块之间我无法实现(nullPointerException)。
Any advice will be appreciated任何建议将被认真考虑
Your description about the packages is hard to understand.你对包的描述很难理解。 But, it most likely is a
Component scanning
issue.但是,它很可能是
Component scanning
问题。 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"})
例如,在
taskexecutor-service
,你可以这样做: @ComponentScan(basePackages = {"com.example.taskexecutor", "com.example.application"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.