尝试将自定义功能添加到Spring数据存储库,但出现UnsatisfiedDependencyException

时间:2018-12-24 16:05:08

标签: java spring-data-jpa

我试图向Spring Data Repository添加几种方法,但出现UnsatisfiedDependencyException

AppUserRepository.java

@Repository
public interface AppUserRepository extends JpaRepository<AppUser,Long>,    AppUserRepositoryCustom {
}

CustomDetailsS​​ervice.java

@Service("userDetailsService")
public class CustomUserDetailsService implements UserDetailsService {

@Autowired
AppUserRepository appUserRepository;

@Override
@Transactional
public UserDetails loadUserByUsername(String usernameOrEmail)
    throws UsernameNotFoundException {
AppUser appUser = appUserRepository.findByUsernameOrEmail(usernameOrEmail, usernameOrEmail)
        .orElseThrow(() ->
                new UsernameNotFoundException("User not found with username or email : " + usernameOrEmail)
        );

return UserPrincipal.  create(appUser);

}

@Transactional
public UserDetails loadUserById(Long id) {
AppUser appUser = appUserRepository.findById(id).orElseThrow(
        () -> new UsernameNotFoundException("User not found with id : " + id)
);

return UserPrincipal.create(appUser);

}

AppUserRepositoryCustom.java

public interface AppUserRepositoryCustom {
Optional<AppUser> findByEmail(String email);
Optional<AppUser> findByUsernameOrEmail(String username, String email); 
}

AppUserRepositoryImpl.java

public class AppUserRepositoryImpl implements AppUserRepositoryCustom {
@Override
public Optional<AppUser> findByEmail(String email) {
    return Optional.empty();
}

@Override
public Optional<AppUser> findByUsernameOrEmail(String username, String email) {
    return Optional.empty();
}

}

但是我遇到了UnsatisfiedDependencyException

由以下原因引起:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ userDetailsS​​ervice”的bean时出错:通过字段“ appUserRepository”表达的不满意的依赖关系;

怎么了?

0 个答案:

没有答案