我有一个非常令人困惑的问题 - 由于某种原因,Spring的bean创建过程试图在我的一个bean上调用自定义init方法并抛出IllegalArgumentException错误,即使我还没有声明bean上的init方法,我也不想要它。
这是在Spring Boot / Batch应用程序中。我已经使用了@SpringBootApplication和@EnableBatchProcessing,我的批量配置使用了额外的@Configuration类来保存各个步骤的配置。
@EnableBatchProcessing
public class MyBatchJobConfig extends DefaultBatchConfigurer {
@Autowired
private MyStepConfig myStep;
//Construction of other steps and the overall job (using the factories)
@Bean
public Step myStep() {
//Step configuration, with the various readers, writers, etc
//accessible through myStep (e.g. myStep.writer() )
}
}
@Configuration
public class MyStepConfig {
@Value("${myStep.propFoo}")
private String foo;
@Value("${myStep.propBazzle}")
private String bazzle;
private final String SOME_CONSTANT = "Constant Bar";
//MyItemWriter is a Spring Batch ItemWriterAdapter<T> implementation
@Bean
public MyItemWriter writer1() {
return new MyItemWriter(foo, SOME_CONSTANT);
}
@Bean
public MyItemWriter writer2() {
return new MyItemWriter(bazzle, SOME_CONSTANT);
}
}
所有这一切都很好,看起来很像我见过的很多例子。我想,没什么不寻常的。它编写得很干净,但是当我尝试运行它时......
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'writer1' defined in class path resource [MyStepConfig.class]: Invocation of init method failed; nested exception is ...
...//stack trace omitted here
Caused by: java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
at org.springframework.util.Assert.notNull(Assert.java:134) ~[spring-core-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at org.springframework.util.Assert.notNull(Assert.java:143) ~[spring-core-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at org.springframework.batch.item.adapter.AbstractMethodInvokingDelegator.afterPropertiesSet(AbstractMethodInvokingDelegator.java:135) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar!/:3.0.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.13.RELEASE.jar!/:4.3.13.RELEASE]
那么我的眼睛失踪了什么?
编辑:这是MyItemWriter的构造函数:
public class MyItemWriter extends ItemWriterAdapter<DataPOJO> {
private String m1;
private String m2;
public MyItemWriter(String one, String two) {
super();
//Obviously, these aren't the actual messages, but the important things
//are that they're not the default message of the requireNotNull message,
//they're different from each other,
//and neither is output in the stack trace.
Objects.requireNonNull(one, "Without One MyItemWriter will not work.");
Objects.requireNonNull(two, "Without Two, MyItemWriter will misbehave.");
this.m1 = one;
this.m2 = two;
}
@Override
public void write(List<? extends DataPOJO> items) throws Exception {
//The remaining code of MyItemWriter, which does not call super(),
//or, frankly, *any* of it's ancestors' methods.
...
}
}
答案 0 :(得分:0)
感谢评论中的所有人。你们都让我把它看作正确的地方。
当我应该实现ItemWriter时,问题是(正如你们所有人猜测的那样)我扩展了ItemWriterAdapter。然后我被现有的代码眼罩蒙蔽了眼睛。无论如何,它现在全部排序。
对于未来的读者 - ItemWriterAdapter不是预先实现的ItemWriter(请注意,它不在* .support包中)。它就是它所说的 - 适配器设计模式的实现,并且应该在您在批处理作业中迭代的项目可以处理它们自己的写入操作时使用。通常,您可能只是想直接实现ItemWriter接口。
再次感谢所有人帮助理顺我。