Spring 使用Crudepositoy从DAO中获取NullPointerException

Spring 使用Crudepositoy从DAO中获取NullPointerException,spring,spring-data,spring-data-jpa,Spring,Spring Data,Spring Data Jpa,我使用crudepository获得了java.lang.NullPointerException 我的项目如下所示: strut2作用 服务 存储库 领域 class Action { @Autowired MyService service; public String execute(){ service.getList(); return "ok"; } } interface Service { List getList(); } @org.spr

我使用crudepository获得了java.lang.NullPointerException

我的项目如下所示:

strut2作用

服务

存储库

领域

class Action {
  @Autowired MyService service;
  public String execute(){
    service.getList();
    return "ok";
  }
}

interface Service {
   List getList();
}

@org.springframework.stereotype.Service
class ServiceImpl implements Service {
  @Autowired MyRepository repo;
  List getList(){
    return (List)repo.findAll();
  }
}
package com.mycompany.repositories;
interface MyRepository extends CrudRepository<MyPojo, String>{}

我也试着自己调试它。我在ServiceImpl类中发现,@Autowired MyRepository为空

看来我找到了答案,谢谢@MartenDeinum from

以下是技巧:

  • 我将struts2用于GUI,因此我需要struts2 spring插件来重写struts2对象工厂,以便spring可以找到struts操作

  • 我为@Service添加了这个配置

    <context:component-scan base-package="com.myproject" />
    
    
    
    基本上我也需要

    <jpa:repositories base-package="com.myproject.repository" />
    
    
    
    寻找积垢


  • 希望这能帮助其他人。

    看来我找到了答案,谢谢@MartenDeinum from

    以下是技巧:

  • 我将struts2用于GUI,因此我需要struts2 spring插件来重写struts2对象工厂,以便spring可以找到struts操作

  • 我为@Service添加了这个配置

    <context:component-scan base-package="com.myproject" />
    
    
    
    基本上我也需要

    <jpa:repositories base-package="com.myproject.repository" />
    
    
    
    寻找积垢


  • 希望这能帮助其他人。

    哪个类实现了MyRepository?您能在这里显示定义吗?@SunilChavan我不必实现MyRepository,Crudepository提供了操作我的POJO的功能您介意添加确切的堆栈跟踪吗?顺便说一句,
    PersistenceAnnotationBeanPostProcessor
    的声明实际上是不必要的。@OliverGeerke嗨,Oliver,谢谢你的评论,堆栈跟踪实际上只告诉我NullPointerException。很高兴在这里看到您^^哪个类实现了MyRepository?您能在这里显示定义吗?@SunilChavan我不必实现MyRepository,Crudepository提供了操作我的POJO的功能您介意添加确切的堆栈跟踪吗?顺便说一句,
    PersistenceAnnotationBeanPostProcessor
    的声明实际上是不必要的。@OliverGeerke嗨,Oliver,谢谢你的评论,堆栈跟踪实际上只告诉我NullPointerException。很高兴在这里见到你^^