首页 文章

@Autowire字段有空指针异常['new'未使用关键字]

提问于
浏览
1

我已经尝试了Why is my Spring @Autowired field null?中提到的解决方案,但问题仍然存在 . 我尝试用@Configurable @Service注释类DevicePojo(下面的代码) .

这是我的bean DistributionConfig.java

@Component
@Configuration
public class DistributionConfig {

    @Qualifier("exponentialDistribution")
    @Bean
    @Scope("prototype")
    public DistributionService exponentialDistribution() {
        return new ExponentiallyDistribute();
    }

    @Qualifier("normalDistribution")
    @Bean
    @Scope("prototype")
    public DistributionService normalDistribution() {
        return new NormallyDistribute();
    }

    @Qualifier("uniformDistribution")
    @Bean
    @Scope("prototype")
    public DistributionService uniformDistribution() {
        return new UniformlyDistribute();
    }
}

JsonFileConfig.java

@Configuration
public class JsonFileConfig {

    private static ObjectMapper mapper=new ObjectMapper();

    @Qualifier("devicesPojo")
    @Bean
    public DevicesPojo[] devicesPojo() throws Exception {
        DevicesPojo[] devicePojo=mapper.readValue(new File(getClass().getClassLoader().getResource("Topo/esnet-devices.json").getFile()),DevicesPojo[].class);
        return devicePojo;
    }


    @Qualifier("linksPojo")
    @Bean
    public LinksPojo[] linksPojo() throws Exception {
        LinksPojo[] linksPojo=mapper.readValue(new File(getClass().getClassLoader().getResource("Topo/esnet-adjcies.json").getFile()),LinksPojo[].class);
        return linksPojo;
    }
}

这是我的DevicePojo,我得到空指针异常 .

@JsonDeserialize(using = DeviceDeserializer.class)
@Component
public class DevicesPojo {

    private String device;
    private List<String> ports;
    private List<Integer> bandwidth;

    @Autowired
    @Qualifier("uniformDistribution")
    private DistributionService uniformDistribution; // Here uniformDistribution is null

    public DevicesPojo(String device, List<String> port, List<Integer> bandwidth) {
        this.device = device;
        this.ports= port;
        this.bandwidth=bandwidth;
        this.uniformDistribution.createUniformDistribution(1000,0,ports.size());
    }

    public String getDevice(){
        return device;
    }

    public String getRandomPortForDevice()
    {
       return ports.get((int)uniformDistribution.getSample());
    }

    public List<String> getAllPorts(){
       return ports;
    }

    public int getBandwidthForPort(String port){    
       return bandwidth.get(ports.indexOf(port));
    }
}

但是,如果我用 private DistributionService uniformDistribution=new UniformDistribution() 替换 private DistributionService uniformDistribution; ,代码工作正常 .

1 回答

  • 1

    这里有各种各样的问题 .
    1.使用JSON反序列化器创建DevicesPojo对象 . Spring没有机会干涉并注入DistributionService .
    2.即使它可能会干扰,它也会失败,因为你试图在构造函数中使用'distributionService'对象 . 场注入仅在构造对象后才起作用 .

    现在关于解决问题 .
    答案很短 - 不要指望POJO中的自动注入 .
    通常,在动态创建的对象(如DevicesPojo)中的依赖关系(如'distributionService')将完全避免 .
    如果您坚持使用它们,请在施工时手动注入它们:

    class DevicesPojoFactory {
        @Autowired @Qualifier("uniformDistribution") 
        private DistributionService uniformDistribution;
        ObjectMapper mapper = new ObjectMapper();
    
        DevicesPojo[] readFromFile(String path) {
             DevicesPojo[] devicePojoArr = mapper.readValue(...);
             for (DevicesPojo dp: devicePojoArr) {
                  dp.setDistribution(uniformDistribution);
             }
        }
    }
    

相关问题