You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot无法找到Mapper Bean问题求助

问题:MapStruct Mapper无法被Spring容器识别为Bean

问题代码与错误信息

Mapper接口定义

@Mapper
@Component
public interface PriceEntityMapper {

    @Mapping(source="brandId", target="brandId")
    @Mapping(source="startDate", target="startDate", qualifiedByName = "timestampToLocalDateTime")
    @Mapping(source="endDate", target="endDate", qualifiedByName = "timestampToLocalDateTime")
    @Mapping(source="priceList", target="priceList")
    @Mapping(source="productId", target="productId")
    @Mapping(source="priority", target="priority")
    @Mapping(source="price", target="price")
    @Mapping(source="curr", target="currency")
    Price priceEntityToPrice(PriceEntity priceEntity);

    @Named("timestampToLocalDateTime")
    default LocalDateTime timestampToLocalDateTime(Timestamp timestamp) {
        return timestamp.toLocalDateTime();
    }
}

Service实现类

@Service
public class PriceServiceImpl implements PriceService {

    private final PriceRepository priceRepository;
    private final PriceEntityMapper priceEntityMapper;

    @Autowired
    public PriceServiceImpl(PriceRepository priceRepository, PriceEntityMapper priceEntityMapper) {
        this.priceRepository = priceRepository;
        this.priceEntityMapper = priceEntityMapper;
    }

    @Override
    public Price getPrice(LocalDateTime applicationDate, Integer productId, Integer brandId) {
        try {
            //PriceEntity priceEntity = priceRepository.findByBrandIdAndProductIdAndStartDateLessThanEqualApplicationDateAndEndDateGreaterThanEqualApplicationDate(brandId, productId, Timestamp.valueOf(applicationDate));
            return priceEntityMapper.priceEntityToPrice(new PriceEntity());
        } catch (Exception e) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Price not found", e);
        }
    }
}

启动错误

Parameter 1 of constructor in com.dharian.application.service.PriceServiceImpl required a bean of type 'com.dharian.infraestructure.mapper.PriceEntityMapper' that could not be found.

已做尝试

  • 为Mapper添加了@Mapper和@Component注解
  • 配置了@ComponentScan扫描com.dharian.infraestructure和com.dharian.application包
  • 调整过@ComponentScan范围、修改注入方式、调整Mapper注解,但问题未解决

问题原因与解决方案

核心原因

只给Mapper接口加@Mapper和@Component无法让Spring识别对应的Bean,因为MapStruct默认不会生成Spring管理的实现类,需要显式指定组件模型为Spring。

修正步骤

  1. 修改@Mapper注解,添加componentModel = "spring"属性,同时可以去掉@Component(因为该属性会自动让生成的实现类带有@Component注解,重复添加可能引发冲突):
@Mapper(componentModel = "spring")
public interface PriceEntityMapper {
    // 原有代码保持不变
}
  1. 确保MapStruct的依赖与注解处理器配置正确:

    • Maven示例(pom.xml):
      <dependencies>
          <dependency>
              <groupId>org.mapstruct</groupId>
              <artifactId>mapstruct</artifactId>
              <version>1.5.5.Final</version> <!-- 使用最新稳定版 -->
          </dependency>
      </dependencies>
      
      <build>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <version>3.8.1</version>
                  <configuration>
                      <source>17</source> <!-- 对应你的JDK版本 -->
                      <target>17</target>
                      <annotationProcessorPaths>
                          <path>
                              <groupId>org.mapstruct</groupId>
                              <artifactId>mapstruct-processor</artifactId>
                              <version>1.5.5.Final</version>
                          </path>
                      </annotationProcessorPaths>
                  </configuration>
              </plugin>
          </plugins>
      </build>
      
  2. 重新编译项目,确保MapStruct生成了正确的Mapper实现类(一般在target/generated-sources/annotations目录下)。

额外注意事项

  • 确认@ComponentScan的范围确实覆盖了Mapper所在的com.dharian.infraestructure.mapper包,你的现有配置已经包含com.dharian.infraestructure,所以无需调整。
  • 不要手动实现Mapper接口,MapStruct会自动生成实现类,手动实现会导致Spring无法注入正确的实例。

内容的提问来源于stack exchange,提问作者Dharian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 16:02:48