1

I need to generate a file called: NEW_NAME_YYYY-MM-DD where YYYY-MM-DD is the current Date. Everything works fine except for when my batch tries to get the propertie tmp.dir it finds nothing and calls the directory null instead. This is the Writer class I am working with :

public class MyFileWriter extends FlatFileItemWriter {

   @Value("${tmp.dir}")
   private String tempDir;

   public MyFileWriter(){
      super();
      setResource();
   }

   public void setResource() {
      SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
      String stringDate = sdf.format(new Date());
      String myPath = tempDir + "/NEW_NAME_" + stringDate + ".csv";
      Resource outputFile = new FileSystemResource(myPath);
      this.setResource(outputFile);
   }

}

and this is the bean definition placed in applicationContext.xml along with the property-placeholder for the properties file:

<bean id="csvWriter" class="org.conters.writer.MyFileWriter" scope="step">
    <property name="lineAggregator">
        <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
            <property name="delimiter" value=";" />
            <property name="fieldExtractor">
                <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                    <property name="names" value="ad,customer,total" />
                </bean>
            </property>
        </bean>
    </property>
    <property name="encoding" value="${csv.encoding}" />
</bean>

Any idea on why the propertie isn't injected??? and as for the properties file called batch.properties in my case it is well declared in the applicationcontext.

Chayma Atallah
  • 725
  • 2
  • 13
  • 30
  • Is `tmp.dir` defined in your `batch.properties` file or are your trying to inject the Java System property `java.io.tmpdir`? I see you use `temporaryDir` in the `setResource` method while the variable is called `tempDir`. Is that intended or it's a typo? Since you defined your bean in XML config, what about adding a setter for `tempDir` in your `MyFileWriter` class and add `` in your XML? IMO this is more consistent than mixing both styles of configuration (annotations and XML). – Mahmoud Ben Hassine Jul 16 '19 at 08:42

0 Answers0