spring-data-jpa 服务中构造函数的参数0需要仓库类型的Bean

bbmckpt7  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(149)

我一直收到此错误消息:
说明:
com.projectName.Service.UserService中构造函数的参数0需要一个类型为“com.projectName.Repositories.AppUserRepository”的Bean,但找不到。
动作:
请考虑在配置中定义一个类型为“com.projectName.Repositories.AppUserRepository”的Bean。
那些是我的课

public interface AppUserRepository extends JpaRepository<AppUser, Long> {
    AppUser findByUsername(String username);
    Optional<AppUser> findById(Long id);
}
public interface IUserService {
    AccountDto saveUser(AccountDto newUser) throws ParseException;
    AppRole saveRole(AppRole role);
    void addRoleToUser(String username, String roleName);
    EmployeeInfoDto getUser(String username);
    AppUser getUserApp(String username);
}

@服务@必需的参数构造函数@事务性的@Slf4j公共类UserService实现IUserService、UserDetailsService {

private final AppUserRepository userRepository;
private final AppRoleRepository roleRepository;
private final PasswordEncoder passwordEncoder;
private ModelMapper modelMapper;
private final AppUserModelAssembler assembler;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    AppUser user = userRepository.findByUsername(username);
    if(user==null){

        throw new UsernameNotFoundException("User is not found in database");
    }
    else {

    }
    Collection<SimpleGrantedAuthority> authorities=new ArrayList<>();
    user.getRoles().forEach(role->{authorities.add(new SimpleGrantedAuthority(role.getName()));});

    return new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPassword(),authorities);
}
@Override
public AccountDto saveUser(AccountDto newUser) throws ParseException {

    AppUser usernameAlreadyExist=userRepository.findByUsername(newUser.getUsername());
    if(usernameAlreadyExist== null)
    {
        AppRole role=roleRepository.findByName("employee");
        newUser.setPassword(passwordEncoder.encode(newUser.getPassword()));
        newUser.getRoles().add(role);
        userRepository.save(assembler.convertToAppUserEntity(newUser));
        return newUser;
    }
    else return null;
}
@Override
public void deleteUser(String username){

    AppUser user=userRepository.findByUsername(username);
    userRepository.deleteById(user.getId());
    log.info("obrisala sam korisnika");
}
@Override
public boolean changePassword(String username, UserChangePasswordDto passwordDto){
    AppUser user=userRepository.findByUsername(username);
    String encodedOldPass=passwordEncoder.encode(passwordDto.getOldPassword());
    boolean isPasswordCorrect=passwordEncoder.matches(passwordDto.getOldPassword(),user.getPassword());
    if(isPasswordCorrect)
    {

        user.setPassword(passwordEncoder.encode(passwordDto.getNewPassword()));
        userRepository.save(user);
        return true;
    }
    else {
        return false;
    }
}
@Override
public EmployeeInfoDto updateProfile(String username, EmployeeInfoDto userDto){
    AppUser user=userRepository.findByUsername(username);
    user.setFirstName(userDto.getFirstName());
    user.setLastName(userDto.getLastName());
    user.setPlaceOfWork(userDto.getPlaceOfWork());
    user.setEmail(userDto.getEmail());
    return assembler.toModel(userRepository.save(user));
}
@Override
public AppRole saveRole(AppRole role) {
    return roleRepository.save(role);
}

@Override
public void addRoleToUser(String username, String roleName) {
    AppUser user=userRepository.findByUsername(username);
    AppRole role=roleRepository.findByName(roleName);
    user.getRoles().add(role);
}

@Override
public EmployeeInfoDto getUser(String username) {
    return assembler.toModel(userRepository.findByUsername(username));
}
public AppUser getUserApp(String username) {
    return userRepository.findByUsername(username);
}

}

sxissh06

sxissh061#

你需要用@Repository注解来注解你的接口AppUserRepository。它允许Spring创建你所缺少的bean。

相关问题