【问题标题】:Spring dependency injection in maven multi modules projectMaven多模块项目中的Spring依赖注入
【发布时间】:2020-10-21 17:09:31
【问题描述】:

我正在构建一个包含 4 个模块的多模块 maven 项目。对于其中两个(一个带有其余控制器,一个带有核心业务逻辑),我需要依赖注入的力量。但根本不工作。这是我的父母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>

.....

子 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>

子 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>

两个项目(服务和核心)里面的包是一样的:com.example.application(服务项目中带有@SpringBootApplication的主类在哪里),com.example.application.restcontroller(控制器在哪里在服务项目中),com.example.application.core(我在核心项目中无法注入的@component在哪里)。

在我拥有的核心项目中,在 com.example.application.core 下有一个 @Component,我将在类中将其注入到 com.example.application.restcontroller 包下的服务项目中。 在同一个项目中和主类的包下,依赖注入工作得很好。在两个不同的模块之间我无法实现(nullPointerException)。

任何建议将不胜感激

【问题讨论】:

  • 扫描的是什么包?
  • 由于我没有写@ComponentScan,我想默认扫描的包是主类下带有SpringBootApplication注解(和运行)的包。所以 com.example.application 和这个下的所​​有包(比如我的 Component 所在的 com.example.application.core )。
  • 如果你不想使用@ComponentScan,你也可以使用spring自动配置,见baeldung.com/spring-boot-custom-auto-configuration

标签: java spring maven


【解决方案1】:

您对包裹的描述很难理解。但是,很可能是Component scanning 问题。确保您正在扫描所有包。例如,在taskexecutor-service 中,您可以执行以下操作: @ComponentScan(basePackages = {"com.example.taskexecutor", "com.example.application"})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    相关资源
    最近更新 更多