Java @Autowire字段存在空指针异常[';未使用新的';关键字]
我已经尝试了中提到的解决方案,但问题仍然存在。我尝试过用@Configurable@Service注释类DevicePojo(下面的代码) 这是我的豆子 DistributionConfig.javaJava @Autowire字段存在空指针异常[';未使用新的';关键字],java,spring,exception,javabeans,Java,Spring,Exception,Javabeans,我已经尝试了中提到的解决方案,但问题仍然存在。我尝试过用@Configurable@Service注释类DevicePojo(下面的代码) 这是我的豆子 DistributionConfig.java @Component @Configuration public class DistributionConfig { @Qualifier("exponentialDistribution") @Bean @Scope("prototype") public D
@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));
}
}
@JsonDeserialize(使用=DeviceDeserializer.class)
@组成部分
公共类设备POJO{
专用字符串设备;
私有列表端口;
私有列表带宽;
@自动连线
@限定词(“统一分布”)
私有分发服务uniformDistribution;//此处uniformDistribution为null
公用设备POJO(字符串设备、列表端口、列表带宽){
这个装置=装置;
这个端口=端口;
这个。带宽=带宽;
createUniformDistribution(1000,0,ports.size());
}
公共字符串getDevice(){
返回装置;
}
公共字符串getRandomPortForDevice()
{
返回端口.get((int)uniformDistribution.getSample());
}
公共列表getAllPorts(){
返回端口;
}
public int getBandwidthForPort(字符串端口){
返回带宽.get(port.indexOf(port));
}
}
但是,如果我替换
私有分发服务uniformDistribution
使用专用分发服务uniformditribution=new uniformditribution()
代码运行正常。这里有一系列问题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);
}
}
}
为什么你认为“映射器”应该“尊重”spring注释?使用
mapper.readValue
时,会生成一个新设备POJO
。一种解决方案是让映射器完成自己的工作,然后在结果上“设置”DistributionService(使用一些@Autowired可以获得DistributionService)。只要我在DevicePojo中添加@Autowired,它就可以正常工作。怎么了?你能详细解释一下吗?某种伪代码可能会有所帮助。我可以在devicedeserialiser中自动连接distributionservice并从那里创建devicespojo的实例吗?“构建对象后发生字段注入”很有意义。这也是我认为可能是错误的。但出于某种希望,我试过了。现在它变得清晰了。感谢您不确定DeviceDeserializer,因为它的实例将由对Spring一无所知的JSON创建。只包装JSON要容易得多。反序列化程序做得并不好。但我还是设法解决了这个问题。