spring ...中构造函数的参数x需要一个类型为“...”的bean,但找不到[已关闭]

6tqwzwtp  于 11个月前  发布在  Spring
关注(0)|答案(1)|浏览(131)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
15天前关门了。
Improve this question
我正在使用Sping Boot 进行个人项目。我收到以下错误:
未找到com.learningapp.backend.AcademixHub.services.UserService中构造函数的参数2需要类型为“com.learningapp.backend.AcademixHub.mappers.UserMapper”的Bean。
下面是UserService.java的代码:

@Service
public class UserService {
     private final UserRepository userRepository; 

        private final  PasswordEncoder passwordEncoder;

        private final UserMapper userMapper;    
        
        
    
    public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder, UserMapper userMapper) {
            super();
            this.userRepository = userRepository;
            this.passwordEncoder = passwordEncoder;
            this.userMapper = userMapper;
        }

     public UserDto register(SignUpDto signupDto) {
         System.out.println("hello man hi hello");
            Optional<User> optionalUser = userRepository.findByEmail(signupDto.getEmail());

            if (optionalUser.isPresent()) {
                throw new AppException("Login already exists", HttpStatus.BAD_REQUEST);
            }

            User user = userMapper.signUpToUser(signupDto);
            user.setPassword(passwordEncoder.encode(signupDto.getPassword()));

            User savedUser = userRepository.save(user);

            return userMapper.toUserDto(savedUser);
        }
     
     public UserDto login(LoginDto loginDto) {
            User user = userRepository.findByEmail(loginDto.getEmail())
                    .orElseThrow(() -> new AppException("Unknown user", HttpStatus.NOT_FOUND));

            if (passwordEncoder.matches(loginDto.getPassword(), user.getPassword())) {
                return userMapper.toUserDto(user);
            }
            throw new AppException("Invalid password", HttpStatus.BAD_REQUEST);
        }

        public UserDto findByEmail(String login) {
            User user = userRepository.findByEmail(login)
                    .orElseThrow(() -> new AppException("Unknown user", HttpStatus.NOT_FOUND));
            return userMapper.toUserDto(user);
        }

}

字符串
下面是UserMapper.java的代码:

@Mapper(componentModel="spring")
@Component
public interface UserMapper {
    
    UserDto toUserDto(User user);
    
    @Mapping(target = "password", ignore = true)
    User signUpToUser(SignUpDto signUpDto);
    
}


如果我从这三个变量中删除final并添加一个默认构造函数,服务器可以正常运行。但是,如果我用POST请求命中register端点,我会得到另一个错误:
[请求处理失败:java.lang.NullPointerException:无法调用“com.learningapp.backend.AcademixHub.repository.UserRepository.findByEmail(String)”,因为“this.userRepository”为null],根本原因是
我该怎么做才能解决这个问题?

xpszyzbs

xpszyzbs1#

Spring抱怨没有找到实现UserMapper的bean。最可能的原因是UserMapperImpl没有由MapStruct annotation processor生成。它应该位于与UserMapper相同的包中,但在target/generated-sources下。
在maven编译(运行mvn compile)后,你发现它了吗?

maven,gradle

为了在使用maven编译期间生成此实现,必须在mavenpom.xml中配置注解处理:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${mapstruct.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

字符串
请参阅https://mapstruct.org/documentation/installation,您也可以在其中找到有关gradle的信息。

IDE支持

在IDE开发期间也可以生成Map器。请参阅https://mapstruct.org/documentation/ide-support/
你也可以看看https://www.baeldung.com/mapstruct来了解MapStruct是如何工作的。
UserMapper上不需要@Component,因为它将自动添加到UserMapperImpl实现中。
您正在使用构造函数注入,这很好,不要添加默认构造函数,因为依赖项不会被注入(所有字段将保持为null,导致您描述的NullPointerException)。

相关问题