【问题标题】:Autowiring Spring Beans by Interface通过接口自动装配 Spring Bean
【发布时间】:2021-05-25 19:04:49
【问题描述】:

我正在尝试在我的项目中创建六边形架构。 模块结构如下:

我不希望域(核心)模块依赖于其他模块。它的逻辑应该通过 api 公开,因此我希望其他模块/客户端,如 rest 模块,仅依赖于 API 模块。

我想通过接口 RestaurantService(来自模块 api)使用 RestaurantServiceImpl(来自模块核心)。 Spring 在为接口 RestaurantService 自动装配 Bean 时遇到问题。这是 RestaurantServiceImpl 应该正确注册为 spring bean:

@Service
public class RestaurantServiceImpl implements RestaurantService {

    private final RestaurantRepository restaurantRepository;

    @Autowired
    public RestaurantServiceImpl(RestaurantRepository restaurantRepository) {
        this.restaurantRepository = restaurantRepository;
    }

    @Override
    public Optional<Restaurant> getById(RestaurantId restaurantId) {
        return restaurantRepository.findById(restaurantId);
    }
}

餐厅管理-api/pom.xml

<parent>
    <artifactId>restaurant-management</artifactId>
    <groupId>com.jc</groupId>
    <version>1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>restaurant-management-api</artifactId>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.3.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.7</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.4</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

餐厅管理核心/pom.xml

<parent>
    <artifactId>restaurant-management</artifactId>
    <groupId>com.jc</groupId>
    <version>1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>restaurant-management-core</artifactId>
<dependencies>
    <!--________________________INTERNAL________________________-->
    <dependency>
        <groupId>com.jc</groupId>
        <artifactId>restaurant-management-api</artifactId>
        <version>1</version>
   </dependency>
    <!--________________________SPRING________________________-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.3.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.7</version>
    </dependency>
   <!--________________________DATABASE________________________-->

    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.5.1</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.4</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

餐厅管理休息/pom.xml

<parent>
    <artifactId>restaurant-management</artifactId>
    <groupId>com.jc</groupId>
    <version>1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>restaurant-management-rest</artifactId>
<dependencies>
    <!--________________________SPRING________________________-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.3.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.3.7</version>
    </dependency>
    <dependency>
        <groupId>com.jc</groupId>
        <artifactId>restaurant-management-api</artifactId>
        <version>1</version>
    </dependency>
</dependencies>

餐厅管理/pom.xml

<artifactId>restaurant-management</artifactId>
<packaging>pom</packaging>
<version>1</version>
<groupId>com.jc</groupId>
<modules>
    <module>restaurant-management-core</module>
    <module>restaurant-management-api</module>
    <module>restaurant-management-rest</module>
</modules>

【问题讨论】:

  • 春天有麻烦了……这是什么意思?错误信息在哪里?如果那条红线是你的意思(这不是 Spring - 这是一个编译错误),那么只需将鼠标悬停在它上面,看看它在说什么
  • @JAsgarov 添加了 poms 和 IntelliJ 错误消息。如果我在模块之间缺少一些依赖关系,请告诉我。
  • 你只需要在restaurant-management/pom.xml中定义一次spring。子模块将自动访问它。现在你有 3 个不同的 spring 依赖项,它们都有不同的 spring 容器。如需进一步参考:baeldung.com/spring-boot-multiple-modules
  • @JAsgarov 感谢您的建议!我这样做了,但它并没有解决我的问题,即 Beans 不可用
  • 请出示您更新后的pom.xmls

标签: java spring maven


【解决方案1】:

实际上我已经看到了问题所在 - 您已经定义了几个模块,但默认情况下模块看不到另一个模块中定义的类。 RestaurantService 位于 Controller 的另一个模块中

您需要将模块添加为 pom.xml 中其他模块的依赖项。如果您在红线上方按“Alt+Enter”,Intellij 应该建议您提供帮助。

您可能还需要确保在父 pom 中设置 Spring 以确保它跨越所有模块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多