我正在尝试制作简单的Web门户,但是当我尝试通过以下方式构建代码时


  mvn全新安装-X


我收到错误代码


  描述:
  com.kapjee.dziennik.service.UserDetailsS​​erviceImpl中的字段userRepository需要找不到“ com.kapjee.dziennik.service.UserRepository”类型的Bean。
  行动:
  考虑在配置中定义类型为“ com.kapjee.dziennik.service.UserRepository”的bean。


下面给你一个项目树。

Tree of project

UserDetailsS​​erviceImpl

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

@Autowired
private UserRepository userRepository;

@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username) throws
UsernameNotFoundException {

 User user = userRepository.findByUsername(username);
   if (user != null) {
      Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
      grantedAuthorities.add(new
      SimpleGrantedAuthority(user.getRole().toString()));
      return new
      org.springframework.security.core.userdetails.User(user.getUsername(),
      user.getPassword(), grantedAuthorities);
  } else {
    throw new UsernameNotFoundException("User was not found!");
  }

 }

}


这是UerService

@Service
public interface UserService {

  void save(User user);

  User findByUsername(String username);
}


和UserRepository

public interface UserRepository extends  JpaRepository<User, Long> {
  User findByUsername(String username);
}


在主班我分享所有背包

@SpringBootApplication
@ComponentScan({"com.kapjee.dziennik"})
@EntityScan("com.kapjee.dziennik.model")
@EnableJpaRepositories("com.kapjee.dziennik.model")

最佳答案

@EnableJpaRepositories("com.kapjee.dziennik.model")


我看到您在模型包中指定了jpa存储库,您还需要包括包含该存储库的软件包,因此,如果您的存储库位于com.kapjee.dziennik.repository包下,请返回上一级

 @EnableJpaRepositories("com.kapjee.dziennik")

10-06 13:03