Spring的@Autowire注释在JpaRepository上返回null

时间:2018-07-25 10:50:02

标签: java spring hibernate spring-data-jpa

我正在使用spring-data-jpa构建Web应用程序,但现在出现错误:

Exception in thread "main" java.lang.NullPointerException 
    at com.example.test.demo.config.service.RegionServiceImpl.getAll(RegionServiceImpl.java:40)
    at com.example.test.demo.DemoApplication.main(DemoApplication.java:45)

存储库:

import com.example.test.demo.config.entity.Region;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RegionRepository extends JpaRepository<Region, Integer> {

}

服务:

@Component
public class RegionServiceImpl implements RegionService {

  @Autowired
  private RegionRepository regionRepository;

和型号:

@Entity
//@Table(name = "geo.region")
@Table(name = "region", schema = "geo")
public class Region implements Serializable {


  @Id
  @GeneratedValue
  @Column(name = "id", length = 6, nullable = false)
  private Integer id;
  @Column(name = "title")
  private String title;

  public Region() {
  }

但是现在我在这一步有了NPE:

@Autowired
private RegionRepository regionRepository;

请帮助! :(

0 个答案:

没有答案