【问题标题】:Serviceclass implementing the jparepository always returns null in spring boot app实现 jparepository 的服务类在 Spring Boot 应用程序中始终返回 null
【发布时间】:2019-05-16 18:56:37
【问题描述】:

我是 Java 和 Spring Boot 的新手。 我创建了一个简单的 spring 应用程序,它使用 JPArepository 从数据库中获取学生详细信息。 以下是 studentDetails 实体:

package com.example.webcustomertracker.entity;

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

@Entity
@Table(name = "StudentDetails")
public class StudentDetails {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY) 
	private Integer StudentID;
	private String Name;
	private String Surname;
	private String City;
	
	public StudentDetails() {}

	public String getName() {
		return Name;
	}

	public void setName(String name) {
		Name = name;
	}

	public String getSurname() {
		return Surname;
	}

	public void setSurname(String surname) {
		Surname = surname;
	}

	public String getCity() {
		return City;
	}

	public void setCity(String city) {
		City = city;
	}

	public StudentDetails(String name, String surname, String city) {
		Name = name;
		Surname = surname;
		City = city;
	}

	@Override
	public String toString() {
		return "StudentDetails [Name=" + Name + ", Surname=" + Surname + ", City=" + City + "]";
	}
	
	

}

以下是 JPARepo:

  package com.example.webcustomertracker.data; 
  
  import org.springframework.data.jpa.repository.JpaRepository;

  import com.example.webcustomertracker.entity.StudentDetails;
  
  public interface StudentDetailsRepository extends JpaRepository<StudentDetails, Integer> 
  { 
	   
  }
 

以下是服务类:

package com.example.webcustomertracker.data;

import java.util.Optional;

import com.example.webcustomertracker.entity.StudentDetails;

public interface StudentDetailsService {

	public abstract Optional<StudentDetails> getStudentDetails(int StudentId);
}

以下是服务类实现

package com.example.webcustomertracker.data;

import java.util.Optional;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.example.webcustomertracker.entity.StudentDetails;

@Component
public class StudentDetailsDataAccess implements StudentDetailsService {

	private StudentDetailsRepository studentDetailsRepository;
	
	public StudentDetailsDataAccess(StudentDetailsRepository theStudentDetailsRepository) {
		this.studentDetailsRepository = theStudentDetailsRepository;				
	}

	@Transactional
	public Optional<StudentDetails> getStudentDetails(int StudentId) {
		// TODO Auto-generated method stub
		
		Optional<StudentDetails> objStud =  this.studentDetailsRepository.findById(StudentId);
		
		
		
		return objStud;
	}
	
	
	
}

以下是启动 spring 框架的主类。我只是想调用服务的功能之一,但服务实例为空并且无法执行。

package com.example.webcustomertracker;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.example.webcustomertracker.data.StudentDetailsDataAccess;
import com.example.webcustomertracker.data.StudentDetailsService;
import com.example.webcustomertracker.entity.StudentDetails;

@SpringBootApplication
public class WebCustomerTrackerApplication {
    
    @Autowired
 	private StudentDetailsService studentDetailsService;

	public Optional<StudentDetails> getTheStudentDetails(int id) {
		return studentDetailsService.getStudentDetails(id);
	}

	public static void main(String[] args) {
		SpringApplication.run(WebCustomerTrackerApplication.class, args); 
		
		Optional<StudentDetails> objStudent = new WebCustomerTrackerApplication().getTheStudentDetails(11);

	}

	
}

以下是我运行代码后遇到的错误:

Exception in thread "main" java.lang.NullPointerException
	at com.example.webcustomertracker.WebCustomerTrackerApplication.getTheStudentDetails(WebCustomerTrackerApplication.java:20)
	at com.example.webcustomertracker.WebCustomerTrackerApplication.main(WebCustomerTrackerApplication.java:26)

【问题讨论】:

  • 你需要自动装配 StudentDetailsS​​ervice
  • 做了............还是同样的问题。

标签: hibernate jpa spring-data-jpa


【解决方案1】:

在控制器层自动装配。

假设您有一个名为 IndexController 的控制器 自动接线。

例如

    StudentDetailsService studentService;
    @Autowired
    public IndexController(StudentDetailsService studentService){
Optional<StudentDetails> objStudent = new studentService.getTheStudentDetails(11);
}

【讨论】:

  • 所以你的意思是说,我不能在 main 方法中使用服务。 JPA 数据将仅与控制器一起使用?
  • 你可以使用,但是你会得到 Nullpointerexception。因为您在 Application.run() 之后立即使用服务,此时未创建服务对象。
【解决方案2】:

另一种解决方案是,

    @SpringBootApplication
public class WebCustomerTrackerApplication {

    @Autowired
    private StudentDetailsService studentDetailsService;

    public Optional<StudentDetails> getTheStudentDetails(int id) {
        return studentDetailsService.getStudentDetails(id);
    }

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(WebCustomerTrackerApplication.class, args); 
        Thread.sleep(1500);
        Optional<StudentDetails> objStudent = new WebCustomerTrackerApplication().getTheStudentDetails(11);

    }

}

在这里等待应用程序启动(所有组件都已加载)。 这个时候你不会因为服务对象创建而得到空指针异常。

【讨论】:

  • 它是依赖注入(构造函数级别)。因为控制器中的构造函数需要服务对象。所以它在创建控制器时实例化服务
  • 开发后如何调用控制器?启动应用程序后,将调用 Main 方法。如何调用控制器?
  • 您是否在应用程序中使用了控制器?
猜你喜欢
  • 2018-02-24
  • 2018-02-12
  • 2017-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
  • 1970-01-01
相关资源
最近更新 更多