我有这门课
@Component
public class Workflow{
具有默认和参数构造函数 像这样
public Workflow(GraphModel graph, JSONObject state, String md5Hash)
throws InvalidGraphException {
this(graph);
this.setState(state);
this.setMd5Hash(md5Hash);
}
现在当我使用
@Autowired
Workflow workflow;
效果不错 但我想用参数调用构造函数。我该如何实现?
请注意我试过这样创建bean
@Configuration
@ComponentScan("com.xyz.abc.*")
public class BlueBerryBeansConfiguration {
@Bean
public Workflow getWorkflow(GraphModel graph, JSONObject getJson,
String getmd5) throws InvalidGraphException {
return new Workflow(graph, getJson, getmd5);
}
@Bean
public JSONObject getJson() {
return new JSONObject();
}
@Bean
public String getmd5() {
return new String();
}
}
在工作流构造函数中
@Autowired
public Workflow(GraphModel graph, JSONObject state, String md5Hash)
throws InvalidGraphException {
this(graph);
this.setState(state);
this.setMd5Hash(md5Hash);
}
public Workflow(GraphModel graph) throws InvalidGraphException {
this.setUnpackedGraph(graph);
}
我收到空指针,异常对我来说很有意义,但我该如何解决 >?
请提供您宝贵的意见。谢谢。
最佳答案
在构造函数上使用 @Autowired
注释应该可以做到。如果它不起作用,请尝试删除默认构造函数(如果有的话)。
关于java - Spring如何调用Argumented Constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31871705/