我一直收到这个错误消息:
描述:
com.projectName.Service.UserService中构造函数的参数0需要类型为"com.projectName.Repositories.AppUserRepository"的bean,但找不到该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);
}
@服务@RequiredArgsConstructor@Transactional@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);
}
}
您需要使用@Repository
注释来注释接口AppUserRepository
。它允许Spring创建一个缺少的bean。