繁体   English   中英

Spring为什么要调用自定义的init方法?

[英]Why is Spring calling a custom init method?

我有一个非常令人困惑的问题-由于某种原因,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.
    ...
  }
}

感谢所有评论。 你们都让我寻找正确的地方。

问题是(大家可能都猜到了)我应该实现ItemWriter时扩展了ItemWriterAdapter。 然后,我对现有的代码闪烁器视而不见。 无论如何,现在都已经排序了。

对于将来的读者-ItemWriterAdapter不是预实现的ItemWriter(请注意,它不在* .support软件包中)。 它就是上面所说的-适配器设计模式的实现,当您在批处理作业中迭代的项目可以处理自己的写操作时,应该使用它。 通常,您可能只想直接实现ItemWriter接口。

再次感谢大家的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM