UserRepo.findAll()”因为“this.userRepo” Spring Boot 中为 null
我正在尝试开发 Spring Boot JWT 身份验证 API。当 Spring Boot 登录页面加载并输入密码时,我收到此错误:
UserRepo.findAll()" because "this.userRepo" is null
如何解决此问题?
服务类
@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class UserServiceImple implements UserService {
private UserRepo userRepo;
private RoleRepo roleRepo ;
@Override
public User saveUser(User user) {
// TODO Auto-generated method stub
//log.info("Saving new user to the database",user.get);
return userRepo.save(user);
}
@Override
public Role saveRole(Role role) {
// TODO Auto-generated method stub
//log.info("Saving new Role {} to the database", role.getName());
return roleRepo.save(role);
}
@Override
public void addRoleToUser(String username, String roleName) {
// TODO Auto-generated method stub
//log.info("Adding role {} to user{}", roleName, username);
User user = userRepo.findByUsername(username);
Role role = roleRepo.findByName(roleName);
user.getRoles().add(role);
}
@Override
public User getUser(String username) {
// TODO Auto-generated method stub
//log.info("Fetching user {}", username);
return userRepo.findByUsername(username);
}
@Override
public List<User> getUsers() {
// TODO Auto-generated method stub
//log.info("Fetching all users");
return userRepo.findAll();
}
}
控制器
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/")
public class userController {
@Autowired
private UserService userService;
@GetMapping("/users")
public ResponseEntity<List<User>>getUsers(){
return ResponseEntity.ok().body(userService.getUsers());
}
}
application.properties
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=my2022
logging.level.org.springframework.security=TRACE
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.formate_sql=true
server.error.include-message=always
I am trying to develop a Spring Boot JWT authentication API. When the Spring Boot login page loads and the password is entered I get this error:
UserRepo.findAll()" because "this.userRepo" is null
How can this be fixed?
service class
@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class UserServiceImple implements UserService {
private UserRepo userRepo;
private RoleRepo roleRepo ;
@Override
public User saveUser(User user) {
// TODO Auto-generated method stub
//log.info("Saving new user to the database",user.get);
return userRepo.save(user);
}
@Override
public Role saveRole(Role role) {
// TODO Auto-generated method stub
//log.info("Saving new Role {} to the database", role.getName());
return roleRepo.save(role);
}
@Override
public void addRoleToUser(String username, String roleName) {
// TODO Auto-generated method stub
//log.info("Adding role {} to user{}", roleName, username);
User user = userRepo.findByUsername(username);
Role role = roleRepo.findByName(roleName);
user.getRoles().add(role);
}
@Override
public User getUser(String username) {
// TODO Auto-generated method stub
//log.info("Fetching user {}", username);
return userRepo.findByUsername(username);
}
@Override
public List<User> getUsers() {
// TODO Auto-generated method stub
//log.info("Fetching all users");
return userRepo.findAll();
}
}
controller
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/")
public class userController {
@Autowired
private UserService userService;
@GetMapping("/users")
public ResponseEntity<List<User>>getUsers(){
return ResponseEntity.ok().body(userService.getUsers());
}
}
application.properties
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=my2022
logging.level.org.springframework.security=TRACE
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.formate_sql=true
server.error.include-message=always
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其添加到您的 UserServiceImple 类中
add this to your UserServiceImple class
userRepo
字段为null
因为UserRepo
(我假设您已将其定义为 Spring bean)不是 注入到类中。要解决这个问题,您需要告诉 Spring 注入该 bean。在 Spring 中注入 bean 有多种方法。推荐的方法是使用 基于构造函数的依赖注入。
要以这种方式注入 Bean,您需要有一个构造函数来接收要注入的 Bean。当您使用 Lombok 的
@RequiredArgsConstructor
注释从任何最终生成构造函数时类中的字段,您只需将字段设置为最终字段,Lombok 将生成您需要的构造函数:这将导致生成带有签名的构造函数
public UserServiceImple(UserRepo userRepo, RoleRepo roleRepo)
,然后 Spring 将使用它来自动装配这些 bean。The
userRepo
field isnull
becauseUserRepo
(which I assume you have defined as a Spring bean) is not injected into the class. To fix this, you need to tell Spring to inject that bean.There are several ways to inject the bean in Spring. The recommended approach is to use Constructor-based dependency injection.
To inject the beans in this manner, you need to have a constructor that takes in the beans to be injected. As you are using Lombok's
@RequiredArgsConstructor
annotation to generate a constructor from any final fields in the class, you need only make the fields final, and Lombok will generate the constructor you need:This will cause a constructor to be generated with signature
public UserServiceImple(UserRepo userRepo, RoleRepo roleRepo)
, which will then be used by Spring to autowire those beans.