春季依赖注入在Maven多模块项目中

huangapple go评论123阅读模式
英文:

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:

....
&lt;groupId&gt;com.example.taskexecutor&lt;/groupId&gt;
&lt;artifactId&gt;taskexecutor&lt;/artifactId&gt;
&lt;parent&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
		&lt;version&gt;2.3.4.RELEASE&lt;/version&gt;
		&lt;relativePath /&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;

....

&lt;modules&gt;
		&lt;module&gt;taskexecutor-service&lt;/module&gt;
		&lt;module&gt;taskexecutor-core&lt;/module&gt;
		&lt;module&gt;taskexecutor-common&lt;/module&gt;
		&lt;module&gt;taskexecutor-rules&lt;/module&gt;
&lt;/modules&gt;

.....

Child pom service:

&lt;parent&gt;
    &lt;groupId&gt;com.example.taskexecutor&lt;/groupId&gt;
    &lt;artifactId&gt;taskexecutor&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;/parent&gt;
&lt;artifactId&gt;taskexecutor-service&lt;/artifactId&gt;

.....

	&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-amqp&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
			&lt;artifactId&gt;lombok&lt;/artifactId&gt;
			&lt;optional&gt;true&lt;/optional&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
			&lt;scope&gt;provided&lt;/scope&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
			&lt;scope&gt;test&lt;/scope&gt;
			&lt;exclusions&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
					&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
				&lt;/exclusion&gt;
			&lt;/exclusions&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.amqp&lt;/groupId&gt;
			&lt;artifactId&gt;spring-rabbit-test&lt;/artifactId&gt;
			&lt;scope&gt;test&lt;/scope&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.example.taskexecutor&lt;/groupId&gt;
			&lt;artifactId&gt;taskexecutor-core&lt;/artifactId&gt;
			&lt;version&gt;${project.version}&lt;/version&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;

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

....
&lt;parent&gt;
    &lt;groupId&gt;com.example.taskexecutor&lt;/groupId&gt;
    &lt;artifactId&gt;taskexecutor&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;/parent&gt;
  &lt;artifactId&gt;taskexecutor-core&lt;/artifactId&gt;

...

&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;

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 = {&quot;com.example.taskexecutor&quot;, &quot;com.example.application&quot;})

英文:

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 = {&quot;com.example.taskexecutor&quot;, &quot;com.example.application&quot;})

huangapple
  • 本文由 发表于 2020年10月22日 01:09:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64468471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定