当前位置: 首页 > 知识库问答 >
问题:

构造函数需要一个找不到的bean类型

华萧迟
2023-03-14
Parameter 1 of constructor in com.subreddit.service.SubredditService required a bean of type 'com.subreddit.mapper.SubredditMapper' that could not be found.

Consider defining a bean of type 'com.subreddit.mapper.SubredditMapper' in your configuration.

以下是服务:

package com.subreddit.service;

import static java.util.stream.Collectors.toList;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.subreddit.dto.SubredditDto;
import com.subreddit.exceptions.SpringRedditException;
import com.subreddit.mapper.SubredditMapper;
import com.subreddit.model.Subreddit;
import com.subreddit.repository.SubredditRepository;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Service
@AllArgsConstructor
@Slf4j

public class SubredditService {
private final SubredditRepository subredditRepository;
private SubredditMapper subredditMapper;

@Transactional
public SubredditDto save(SubredditDto subredditDto) {
    Subreddit save = subredditRepository.save(subredditMapper.mapDtoToSubreddit(subredditDto));
    subredditDto.setId(save.getId());
    return subredditDto;
}

@Transactional(readOnly = true)
public List<SubredditDto> getAll() {
    return subredditRepository.findAll()
            .stream()
            .map(subredditMapper::mapSubredditToDto)
            .collect(toList());
}

public SubredditDto getSubreddit(Long id) {
    Subreddit subreddit = subredditRepository.findById(id)
            .orElseThrow(() -> new SpringRedditException("No subreddit found with ID - " + id));
    return subredditMapper.mapSubredditToDto(subreddit);
}
}

下面是映射器:

package com.subreddit.mapper;

import java.util.List;

import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import com.subreddit.dto.SubredditDto;
import com.subreddit.model.Post;
import com.subreddit.model.Subreddit;

import lombok.RequiredArgsConstructor;


@Mapper(componentModel = "spring")
public interface SubredditMapper {

    @Mapping(target = "numberOfPosts", expression = "java(mapPosts(subreddit.getPosts()))")
    SubredditDto mapSubredditToDto(Subreddit subreddit);

    default Integer mapPosts(List<Post> numberOfPosts) {
        return numberOfPosts.size();
    }

    @InheritInverseConfiguration
    @Mapping(target = "posts", ignore = true)
    Subreddit mapDtoToSubreddit(SubredditDto subredditDto);
}

共有1个答案

毕浩渺
2023-03-14

造成这个错误的原因是Spring没有找到bean映射器(Spring在构建过程中没有找到映射器实现),并且使用mapstruct-processor来生成它。

在pom.xml中,将dependecy和annotationProcessorPaths路径部分添加到maven-compiler-plugin插件的配置部分。添加后,您的pom文件应该如下所示:

<dependency>
   <groupId>org.mapstruct</groupId>
   <artifactId>mapstruct</artifactId>
   <version>1.3.1.Final</version>  
</dependency>
...
<build>
    <plugins>
       ....
       <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.5.1</version>
          <configuration>
             <source>1.8</source>
             <target>1.8</target>
             <annotationProcessorPaths>
                <path>
                   <groupId>org.mapstruct</groupId>
                   <artifactId>mapstruct-processor</artifactId>
                   <version>1.3.1.Final</version>
                </path>
             </annotationProcessorPaths>
           </configuration>
       </plugin>
       ....

您所要做的就是对pom.xml文件进行清洁安装。这将生成实现类。清除安装步骤:

  1. 右键单击pom.xml
  2. 展开运行方式
  3. 选择Maven Build
  4. 为命令设置目标clean install
  5. 应用>运行>关闭
 类似资料: