英文:
Spring dependency injection in maven multi modules project
问题
我正在构建一个具有4个模块的多模块Maven项目。对于其中两个模块(具有REST控制器的模块和核心业务逻辑的模块),我需要使用依赖注入的功能。但是依赖注入根本不起作用。这是我的父POM:
父POM部分省略...
模块部分:
<modules>
<module>taskexecutor-service</module>
<module>taskexecutor-core</module>
<module>taskexecutor-common</module>
<module>taskexecutor-rules</module>
</modules>
子模块 service:
<parent>
<groupId>com.example.taskexecutor</groupId>
<artifactId>taskexecutor</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>taskexecutor-service</artifactId>
依赖部分省略...
子模块 core(其中有一个无法注入到service项目的类):
部分省略...
<parent>
<groupId>com.example.taskexecutor</groupId>
<artifactId>taskexecutor</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>taskexecutor-core</artifactId>
依赖部分省略...
这两个项目(service和core)内部的包名相同:com.example.application(在service项目中带有@SpringBootApplication注释的主类),com.example.application.restcontroller(在service项目中的控制器),com.example.application.core(在核心项目中无法在service项目的com.example.application.restcontroller包中注入的@Component)。
在core项目内部,在com.example.application.core下有一个@Component,我想将其注入到位于com.example.application.restcontroller包下的service项目的类中。
在同一项目内且在主类的包下,依赖注入完全正常。但在两个不同的模块之间无法实现(出现NullPointerException)。
将不胜感激地接受任何建议。
英文:
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
答案1
得分: 4
你对这些包的描述很难理解。但很可能是组件扫描
的问题。确保你正在扫描所有的包。例如,在taskexecutor-service
中,你可以这样做:
@ComponentScan(basePackages = {"com.example.taskexecutor", "com.example.application"})
英文:
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"})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论