没有可用的“Mapper.class”类型的合格bean

问题描述 投票:0回答:0

我有一个使用springboot 2.5.14版本的项目。当我尝试运行它时会出现类似

的错误

异常:org.springframework.beans.factory.UnsatisfiedDependencyException。消息:创建名称为“requestFilter”的 bean 时出错:通过字段“logService”表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'logServiceImpl' defined in file ...: Unsatisfied dependency expressed through constructor parameter 1;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用类型为“demo.mapper.LogMapper”的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选者。依赖注解:{}

Mapstruct 版本:1.4.2.Final 和 Lombok 是 1.8.24

    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>${mapstruct.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>${mapstruct.version}</version>
    </dependency>
    
    
    <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>  <!-- IMPORTANT - LOMBOK BEFORE MAPSTRUCT -->
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version>
                    </path>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
        

我的代码如下:

@Component
@Slf4j
public class RequestFilter implements Filter {

  @Autowired
  private LogService logService;

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    log.info("servlet filter initialized..");
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse httpResponse, FilterChain filterChain) throws IOException, ServletException {
    try {
      filterChain.doFilter(request, httpResponse);
    } finally {
      httpAnalyze(request);      
    }
  }
  
  private void httpAnalyze(ServletRequest request) {

    HttpServletRequest httpRequest = (HttpServletRequest) request;

    // It makes some analysis and log them
    logService.insertLog("...", "...");

  }
}
@RequiredArgsConstructor
@Service
@Slf4j
public class LogServiceImpl implements LogService {

  private final LogRepository logRepository;
  private final LogMapper logMapper;
  
  private Long insertLog(String note, String content) {

    LogDtoBuilder logBuilder = LogDto.builder();

    InsuredNoContextData insuredNoContextData = HclmRequestContextHolder.getInsuredNoContextData();

    initInsuredNo(logBuilder, insuredNoContextData);

    LogDto logDto = logBuilder.note(getNote(note)).content(content).build();

    Long logId = logRepository.save(logMapper.convertToEntity(logDto)).getLogId();


    return logId;
  }
}
@Mapper(componentModel = "spring")
public interface LogMapper extends BaseMapper<LogEntity, LogDto, LogDto> {

}

我在网上尝试了很多解决方案,但找不到。

mapstruct
© www.soinside.com 2019 - 2024. All rights reserved.