问题描述
我正在尝试在我的项目中进行六边形架构。 模块结构如下所示:
我不希望域(核心)模块依赖于其他模块。它的逻辑应该通过 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);
}
}
restaurant-management-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>
餐厅管理rest/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>
解决方法
实际上我已经看到了问题所在——您已经定义了几个模块,但默认情况下,模块没有看到在另一个模块中定义的类。 RestaurantService
位于 Controller
您需要将模块作为依赖项添加到其他模块 pom。如果您只是在红线上按“Alt+Enter”,Intellij 应该建议您提供帮助。
您可能还需要确保在父 pom 中设置 Spring 以确保它跨越所有模块。