0

I am trying to create a CustomWriter that contains multiple FlatFileItemWriters. The configuration is by xml. I want the FlatFileItemWriters to be injected into the CustomWriter depending on values in the jobParameters; Like this-

<bean id="customWriter" class="com.company.writer.CustomDataWriter" scope="step">
    <property name="chapatiDataWriter" ref="#{jobParameters['chapatiSection'] != null ? chapatiDataWriter : null}"/>
    <property name="anotherDataWriter" ref="anotherDataWriter"/>
    <property name="batchJobConfiguration" value="#{jobParameters['product_configuration']}"/>
</bean>

When I try to run the application, I get;

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.customWriter' defined in URL [file:./conf/context.xml]: Cannot resolve reference to bean '#{jobParameters['chapatiSection'] != null ? chapatiDataWriter : null}' while setting bean property 'chapatiDataWriter'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.batch.item.file.FlatFileItemWriter@19cb8309' available

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.batch.item.file.FlatFileItemWriter@19cb8309' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:805)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1279)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330)
    ... 52 common frames omitted

How do I conditionally inject the chapatiDataWriter depending on the jobParameters?

theeGwandaru
  • 400
  • 2
  • 14

1 Answers1

0

Even though SpEL is very powerful, I do not recommend using complex code in strings. I would use a FactoryBean to create the custom writer and use the factory bean in the XML configuration:

<bean id="customWriter" class="com.company.writer.CustomDataWriterFactoryBean" scope="step"/>
Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50