简体   繁体   English

Service 中构造函数的参数 0 需要一个 Repository 类型的 bean

[英]Parameter 0 of constructor in Service required a bean of Repository type

I keep getting this error message:我不断收到此错误消息:

Description:描述:

Parameter 0 of constructor in com.projectName.Service.UserService required a bean of type 'com.projectName.Repositories.AppUserRepository' that could not be found. com.projectName.Service.UserService 中构造函数的参数 0 需要找不到类型为“com.projectName.Repositories.AppUserRepository”的 bean。

Action:行动:

Consider defining a bean of type 'com.projectName.Repositories.AppUserRepository' in your configuration.考虑在您的配置中定义“com.projectName.Repositories.AppUserRepository”类型的 bean。

those are my classes这些是我的课

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);
}

@Service @RequiredArgsConstructor @Transactional @Slf4j public class UserService implements IUserService, UserDetailsService { @Service @RequiredArgsConstructor @Transactional @Slf4j public class 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);
}

} }

You need to annotate your interface AppUserRepository with @Repository annotation.您需要使用@Repository注释来注释您的接口AppUserRepository It allows Spring to create a bean your are missing.它允许 Spring 创建您缺少的 bean。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 服务中构造函数的参数 0 需要无法找到类型存储库的 bean - Parameter 0 of constructor in service required a bean of type repository that could not be found “服务”中构造函数的参数 0 需要找不到类型为“存储库”的 bean - Parameter 0 of constructor in 'Service' required a bean of type 'Repository' that could not be found *Service 中构造函数的参数 0 需要找不到类型为“*Repository”的 bean - Parameter 0 of constructor in *Service required a bean of type '*Repository' that could not be found 构造函数的参数 0 需要一个类型的 bean - Parameter 0 of constructor required a bean of type Controller 中构造函数的参数 0 需要一个找不到的 Service class 类型的 bean - Parameter 0 of constructor in Controller required a bean of type Service class that could not be found “.service”中构造函数的参数 0 需要找不到类型为“.model”的 bean - Parameter 0 of constructor in '.service ' required a bean of type '.model ' that could not be found 构造函数的参数 0 需要一个类型为“FileStoragePropertiesAedImages”的 bean - Parameter 0 of constructor required a bean of type 'FileStoragePropertiesAedImages' &quot;&quot; 中构造函数的参数 0 需要一个无法找到的类型为 &quot;&quot; 的 bean - Parameter 0 of constructor in "" required a bean of type "" that could not be found “ ”中构造函数的参数 0 需要找不到类型为“ ”的 bean - Parameter 0 of constructor in ' ' required a bean of type ' ' that could not be found Spring - 服务中构造函数的错误参数 0 需要一个无法找到的配置类型的 bean - Spring - Error Parameter 0 of constructor in Service required a bean of type Configuration that could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM