如何在反序列化类中自动装配?
这是映射类:
@Component
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonNaming(PropertyNamingStrategy.PascalCaseStrategy.class)
public class Container {
@Autowired
private CalcResourceUsage calcResourcePercentage;
private ContainerStats containerStats;
@Autowired
private AverageResourceUsage averageResourceUsage;
public Container(){
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
private String Id;
private List<String> Names;
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public List<String> getNames() {
return Names;
}
public void setNames(List<String> names) {
Names = names;
}
public CalcResourceUsage getCalcResourcePercentage() {
return calcResourcePercentage;
}
public AverageResourceUsage getAverageResourceUsage() {
return averageResourceUsage;
}
public ContainerStats getContainerStats() {
return containerStats;
}
public void setContainerStats(ContainerStats containerStats) {
this.containerStats = containerStats;
}
}
所以我使用jackson将对象映射到这个类中,使用以下代码行:
Container[] containers = syncRestTemplate.getForObject(address + "json", Container[].class);
然后我有一个数组,我需要对每个容器进行一些计算,如果我可以自动装配CalcResourceUsage
和AverageResourceUsage
,我可以为每个容器做一些计算。
目前getAverageResourceUsage()
和getCalcResourcePercentage()
返回null
答案 0 :(得分:1)
您可以将反序列化的对象传递给AutowireCapableBeanFactory
进行连线(使用autowireBean()
方法)。
示例:
@Autowired
private AutowireCapableBeanFactory beanFactory;
public void doDeserialization() {
Container[] containers = // get them via jackson
for (Container container : containers) {
beanFactory.autowireBean(container);
// container will now have its dependencies autowired.
}
}