[英]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:
你需要一些东西:
@Override
public void run(String... args) {
serv.getAllUsersById();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.