Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么Spring要调用自定义init方法?_Java_Spring - Fatal编程技术网

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

Java 为什么Spring要调用自定义init方法?,java,spring,Java,Spring,我有一个非常令人困惑的问题——出于某种原因,Spring的bean创建过程试图在我的一个bean上调用一个定制的init方法,并抛出一个IllegalArgumentException错误,尽管我没有在bean上声明init方法,也不需要 这在Spring引导/批处理应用程序中。我使用了@SpringBootApplication和@EnableBatchProcessing,我的批处理配置是使用额外的@configuration类来保存各个步骤的配置 @EnableBatchProcessin

我有一个非常令人困惑的问题——出于某种原因,Spring的bean创建过程试图在我的一个bean上调用一个定制的init方法,并抛出一个IllegalArgumentException错误,尽管我没有在bean上声明init方法,也不需要

这在Spring引导/批处理应用程序中。我使用了@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);
  }
}
那么我的眼睛缺了什么

编辑:这是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.
    ...
  }
}
公共类MyItemWriter扩展ItemWriterAdapter{
私有字符串m1;
私有字符串m2;
公共MyItemWriter(字符串1、字符串2){
超级();
//显然,这些不是真正的信息,而是重要的东西
//它们不是RequireNotFull消息的默认消息,
//它们彼此不同,
//并且两者都不会在堆栈跟踪中输出。
requirennull(一个,“没有一个MyItemWriter将无法工作”);
requirennull(两个,“没有两个,MyItemWriter将行为不端。”);
这1.m1=1;
这1.m2=2;
}
@凌驾

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

问题是(你们可能都猜到了),我扩展了ItemWriterAdapter,而我本应该实现ItemWriter。然后我被现有的代码闪烁器弄瞎了。不管怎样,现在一切都好了

对于未来的读者-ItemWriterAdapter不是预先实现的ItemWriter(请注意,它不在*.support包中)。这就是它在tin上所说的内容—适配器设计模式的实现,当您在批处理作业中迭代的项可以处理它们自己的写入操作时,应该使用它。通常,您可能只希望直接实现ItemWriter接口


再次感谢大家帮助我理顺。

MyItemWriter
是关于什么的?添加最少的代码。呃,我真的不担心,因为代码在进入MyItemWriter的构造函数之前就失败了。但是,当然,我会添加MyItemWriter的构造函数。它不是在进入构造函数时失败的,那部分已经失败了如果您看到此异常,则传递。异常会告诉您出了什么问题。您正在扩展一个类,而您可能不理解该类,该类需要设置一些对象/参数以正确运行。您没有设置这些属性,因此也没有设置验证规则(显然是在触发
AbstractMethodInvokingDelegator
AfterPropertieSet
方法后应用的。ItemWriterAdapter()扩展了AMID。AMID和IWA除了默认构造函数之外没有任何其他内容。MyItemWriter重写ItemWriterAdapter的write()方法,该方法应完全忽略默认方法,默认方法调用AMID.AMID具有setter。afterPropertiesSet方法检查是否已调用setter来初始化bean。由于未调用setter,因此在调用afterPropertiesSet时,bean的状态不正确。
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.
    ...
  }
}