我是春天的新手。为了尝试学习它,我按照Josh Long的示例创建了OAuth服务器。他的示例与我正在处理的代码之间的区别在于,我尝试使用MySQL而不是内存数据库。我将相关文件保留在帖子中。
提前致谢。
哦,这是视频https://www.youtube.com/watch?v=EoK5a99Bmjc的链接
这是我实现存储库的课程
@Service
public class AccountRepositoryImpl implements UserDetailsService {
@Autowired
private final AccountRepository accountRepository;
public AccountRepositoryImpl(AccountRepository accountRepository){
this.accountRepository = accountRepository;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return accountRepository.findByUsername(username)
.map(account -> new User(account.getUsername(),
account.getPassword(), account.isActive(), account.isActive(), account.isActive(), account.isActive(),
AuthorityUtils.createAuthorityList("ROLE_ADMIN", "ROLE_USER")
))
.orElseThrow(() ->new UsernameNotFoundException ("Couldn't find the username " + username + "!"));
}
}
这是仓库
public interface AccountRepository extends JpaRepository<Account, Long> {
Optional<Account> findByUsername(String username);
}
最佳答案
这两个部分完全相同:
@Autowired
private final AccountRepository accountRepository;
和
public AccountRepositoryImpl(AccountRepository accountRepository){
this.accountRepository = accountRepository;
}
当您在accountRepository上方添加@Autowired批注时,Spring会自动将AccountRepository实例注入到AccountRepositoryImpl类中。因此,您需要删除第二个选择,因为它很可能与@Autowired注释冲突。
编辑-----------------------------
@Component
public class ApplicationStartUp implements ApplicationListener<ApplicationReadyEvent> {
@Autowired
private AccountRepository accountRepository;
@SuppressWarnings("null")
@Override
public void onApplicationEvent(final ApplicationReadyEvent event) {
Account account = new Account("sPatel", "Spring", true);
accountRepository.save(account);
return;
}
}