0
   <bean id="FileWriter" class="com.sample.FileWriter">
          <constructor-arg value="${path.to.output}"/>
          <constructor-arg value="${filename}"/>
   </bean>

I would like to pass in the file name with today's date from a property file using spring so that it is not hard coded in the class. Is this possible?

File name I'm looking to pass in F_IN_1243_MMDDYYYY.xml where MMDDYYYY is today's date?

M06H
  • 1,675
  • 3
  • 36
  • 76
  • How about creating bean that will have static method for generating file name according to your format and then injecting bean property in the xml by SpringEl – ema May 08 '15 at 14:19

3 Answers3

1

You should be able to make your bean prototyped scope and include a java method call in your property expression. Something like:

   <bean id="FileWriter" class="com.sample.FileWriter" scope="prototype">
          <constructor-arg value="${path.to.output}"/>
          <constructor-arg value="#{T(Utils).filename()}"/>
   </bean>

Where Utils.filename () is a utility method that calls SimpleDateFormat and composes the filename.

redge
  • 1,182
  • 7
  • 6
0

Any reason you can't get the current date of a Calendar object in the constructor or other method of the FileWriter and use the passed in filename as the file prefix?

NinePlanFailed
  • 385
  • 4
  • 11
0

You can use Spring Expression Language (SpEL).

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html

You should be able to do something like that:

<constructor-arg value="#{new SimpleDateFormat('MMddyyyy').format(new Date())}"/>

Use # and not $ and add the necessary dependencies.

barbarian
  • 101
  • 1
  • 3