简体   繁体   English

我的 spring-boot 应用程序中的存储库返回 null

[英]My repository in my spring-boot application is returning null

Hey I am pretty new to using the Spring Framework and was just trying to get an application to work for practice but I am getting the following error in my stack trace: Cannot invoke "com.gabriel.schoolapp.repository.UsersRepo.findAll()" because "this.usersRepo" is null嘿,我对使用 Spring 框架还很陌生,只是想让一个应用程序用于练习,但我在堆栈跟踪中收到以下错误:无法调用“com.gabriel.schoolapp.repository.UsersRepo.findAll() " 因为 "this.usersRepo" 是 null

This is my model layer for the Users:这是我的 model 用户层:

package com.gabriel.schoolapp.models;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
@Entity
@Table(name = "users")

public class Users {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer usersID;
    
    @Column(nullable = false)
    private String firstName;

    public void FirstName(String firstName){
        this.firstName = firstName;
    }

    @Column(nullable = false)
    private String lastName;

    public void ClassDesc(String lastName){
        this.lastName = lastName;
    }

    @Column(nullable = false)
    private String username;

    public void Username(String username){
        this.username = username;
    }
}

This is my repository layer:这是我的存储库层:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.gabriel.schoolapp.models.Users;

@Repository
public interface UsersRepo extends JpaRepository<Users, Integer>{

    Users findByUsername(String firstName);
    
}

And this is my service layer:这是我的服务层:

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gabriel.schoolapp.models.Users;
import com.gabriel.schoolapp.repository.UsersRepo;

@Service
public class UsersService {
    @Autowired
    private UsersRepo usersRepo;

    
    @Autowired
    public UsersService(UsersRepo usersRepo){
        
        this.usersRepo = usersRepo;
    }

    public UsersService(){

    }

     public Users createUser(Users user){
        Users checkIfUserInDb = usersRepo.findByUsername(user.getFirstName());

        if(checkIfUserInDb != null){
            System.out.println("Username already in DB");
            return null;
        }
        System.out.println("User is valid");
        return usersRepo.save(user);
    } 

    public List<Users> getAllUsersById(){
        return this.usersRepo.findAll();
    }
}

Whenever I try to call a method from the service layer like so:每当我尝试像这样从服务层调用方法时:

@SpringBootApplication
public class SchoolAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(SchoolAppApplication.class, args);
        
        UsersService serv = new UsersService();
        serv.getAllUsersById();
        
    }

}

It returns with the aforementioned error (this.usersRepo is null)它返回上述错误(this.usersRepo 为空)

Any help would be greatly appreciated thank you!!任何帮助将不胜感激谢谢!

The UserService you created is not managed by Spring, hence the repo is not autowired.您创建的 UserService 不是由 Spring 管理的,因此 repo 不是自动装配的。 You need a few things:你需要一些东西:

  1. implement CommandLineRunner in SchoolAppApplication在 SchoolAppApplication 中实现 CommandLineRunner
  2. Autowire your service in SchoolAppApplication在 SchoolAppApplication 中自动装配您的服务
  3. Override method run in SchoolAppApplication and run your service there:覆盖在 SchoolAppApplication 中运行的方法并在那里运行您的服务:
    @Override
    public void run(String... args) {
        serv.getAllUsersById();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以启动嵌入在我的 spring-boot 应用程序中的 Axon 服务器? - Is it possible to start the Axon server embedded in my spring-boot application? 为什么我的 Redis 缓存不能在我的 Spring-Boot/Spring-Data 应用程序中工作? - Why isn't my Redis cache working in my Spring-Boot/Spring-Data application? 使用spring-boot在我的项目中找不到@Autowired - @Autowired is not found in my project with spring-boot 我的Service类中的spring-boot @autowired接口(CrudRepository)=&gt;空指针异常 - spring-boot @autowired interface (CrudRepository) in my Service class => Null pointer exception 如何为嵌入在我的 spring-boot 应用程序中的 Web 服务器(Tomcat)配置低级套接字属性? - How to configure low-level socket properties for a web server (Tomcat) embedded in my spring-boot application? 如何在上下文创建错误后使用spring-boot-starter-actuator完成弹簧启动应用程序? - How to make my spring-boot application with spring-boot-starter-actuator finish after context creation error? 如何在不继承&#39;spring-boot-starter-parent&#39;模块的情况下测试我的Spring-Boot应用程序? - How can I test my Spring-Boot application without inheriting 'spring-boot-starter-parent' module? 我的 spring-boot 程序在属性中没有 JavaBuilder - My spring-boot program doesn't have the JavaBuilder in the Properties 我无法创建我的自定义 spring-boot 启动器 - I can't create my custom spring-boot starter @Requestbody没有得到我的JSON对象Spring-Boot - @Requestbody is not Getting my JSON object Spring-Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM