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

@Autowire field has Null Pointer Exception ['new' keyword not used]

我已经尝试过Why is my Spring @Autowired field null?中提到的解决方案,但问题仍然存在。我已经尝试使用 @Configurable @Service.

注释 class DevicePojo(下面的代码)

这是我的豆子 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; 替换为 private DistributionService uniformDistribution=new UniformDistribution(),代码工作正常。

这里有很多问题。
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);
         }
    }
}