UserRepo.findAll()”因为“this.userRepo” Spring Boot 中为 null

发布于 2025-01-11 07:27:55 字数 2502 浏览 0 评论 0原文

我正在尝试开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔女人霸气范 2025-01-18 07:27:55

将其添加到您的 UserServiceImple 类中

@Autowired
public UserServiceImple(UserRepo userRepo,RoleRepo roleRepo){this.userRepo = userRepo;this.roleRepo = roleRepo;}

add this to your UserServiceImple class

@Autowired
public UserServiceImple(UserRepo userRepo,RoleRepo roleRepo){this.userRepo = userRepo;this.roleRepo = roleRepo;}
扮仙女 2025-01-18 07:27:55

userRepo 字段为 null 因为 UserRepo (我假设您已将其定义为 Spring bean)不是 注入到类中。要解决这个问题,您需要告诉 Spring 注入该 bean。

在 Spring 中注入 bean 有多种方法。推荐的方法是使用 基于构造函数的依赖注入

要以这种方式注入 Bean,您需要有一个构造函数来接收要注入的 Bean。当您使用 Lombok 的 @RequiredArgsConstructor 注释从任何最终生成构造函数时类中的字段,您只需将字段设置为最终字段,Lombok 将生成您需要的构造函数:

@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class UserServiceImple implements UserService {
    private final UserRepo userRepo;
    private final RoleRepo roleRepo;
    // ...
}

这将导致生成带有签名的构造函数 public UserServiceImple(UserRepo userRepo, RoleRepo roleRepo),然后 Spring 将使用它来自动装配这些 bean。

The userRepo field is null because UserRepo (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:

@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class UserServiceImple implements UserService {
    private final UserRepo userRepo;
    private final RoleRepo roleRepo;
    // ...
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文