gpt4 book ai didi

java - Spring Boot找不到该类型的bean

转载 作者:行者123 更新时间:2023-11-30 02:24:53 26 4
gpt4 key购买 nike

我正在尝试制作简单的门户网站,但是当我尝试通过

构建代码时

mvn clean install -X

我收到错误代码

Description: Field userRepository in com.kapjee.dziennik.service.UserDetailsServiceImpl required a bean of type 'com.kapjee.dziennik.service.UserRepository' that could not be found. Action: Consider defining a bean of type 'com.kapjee.dziennik.service.UserRepository' in your configuration.

下面给你一个项目树。

Tree of project

用户详细信息服务Impl

@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!");
}

}

}

这是优尔服务

@Service
public interface UserService {

void save(User user);

User findByUsername(String username);
}

和用户存储库

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") 

关于java - Spring Boot找不到该类型的bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45902786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com