【问题标题】:Getting NullpointerException with autowiring in springboot (weird scenario)在springboot中使用自动装配获取NullpointerException(奇怪的场景)
【发布时间】:2018-11-22 12:27:39
【问题描述】:

我知道这个特定问题有很多答案,并且我知道可能发生这种情况的各种情况。

但我想我错了,鉴于我的情况,我不知道所有可能的组合。 让我分享一下sn-ps的代码,让代码自己说话。

控制器类

@RestController
public class ReportController {

    @Autowired
    ReportFactory reportFactory;

    //RequestMapping goes here!
    public ResponseEntity<Report> getReport() {

        ReportGenerator<?> reportGenerator = reportFactory.getReportGenerator("order"); //line# 43
        return getSuccessResponse(reportGenerator.getReport(companyGUID, startDate, endDate)); //geting the exception here saying reportGenerator is null!
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private <T> ResponseEntity getSuccessResponse(T response) {
        return new ResponseEntity(response, HttpStatus.OK);
    }
}

我收到了reportGenerator,它位于第 43 行,它将我带到 ReportFactory,您可以看到它应该是autowired

ReportFactory.java

@Component
public class ReportFactory {

    @Autowired
    Map<String, ReportGenerator> reportList;

    public ReportGenerator<?> getReportGenerator(String reportType) {

        return reportList.get(reportType.toLowerCase());
    }
} 

正如你所看到的,这个类被Component注解了,它还有一个reportList的依赖,这个bean来自下面的类。

ReportGeneratorConfig.java

 @Configuration
    public class ReportGeneratorConfig {

        @Autowired
        BeanFactory factory;

        @Bean
        public Map<String, ReportGenerator> reportList() {

            HashMap<String, ReportGenerator> reportList = new HashMap<String, ReportGenerator>();
            reportList.put("order", factory.getBean(OrderReportGeneratorImpl.class)); 
            //OrderReportGeneratorImpl implements ReportGenerator
            return reportList; //created the bean which should be autowired wherever needed!
        }
    }

当我在调试中运行代码时,我可以看到 config 类实际上正在创建 bean 并将其自动装配到 ReportFactory。 但是,相同的 ReportFactory bean 没有自动连接到控制器类。

除此之外,在所有 bean 初始化完成后,当我检查调试值时,reportFactory 实例在 controller 中的 reportlist 实例中具有空值

reportlist 有两个完全不同的条目,在reportFactory 类中从未提及。

有了这些请帮帮我!

附:这是一个 webapp,我正在使用 springBoot。

【问题讨论】:

  • 发布您的错误信息。是说 ReportFactory 为 null 还是 ReportFactory 中使用的列表为 null?
  • 得到NullPointerEXceptionreportGenerator 在第44 行为空。提到了第43 行
  • 您是否尝试过 Map 上方的 @Resource 而不是 autowired
  • @bibliophilsagar @Resource@Autowired 相同,不同之处在于找出需要注入的 bean 所采用的执行路径。 @Resource 将首先按名称缩小搜索范围,然后按类型缩小搜索范围,最后按限定符缩小搜索范围。在您的情况下(我在猜测),您添加的 bean(OrderReportGeneratorImpl) 在添加到列表时尚未准备好。所以如果你把它改成@Resource(name="reportList") 就可以了。
  • 对于reportList,Spring 将自动装配映射中实现ReportGenerator 的所有实例,并以bean 名称作为键。未使用您返回地图的 bean 方法。

标签: java spring spring-mvc spring-boot autowired


【解决方案1】:

对于以后可能会访问这里的人。

根据 mallikarjun 的建议,以下更改帮助了我。

ReportFactory.java

@Component
public class ReportFactory {

    @Resource // It was autowired here
    Map<String, ReportGenerator> reportList;

    public ReportGenerator<?> getReportGenerator(String reportType) {

        return reportList.get(reportType.toLowerCase());
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2015-07-14
    • 2015-07-14
    相关资源
    最近更新 更多