Announcement Announcement Module
Collapse
No announcement yet.
Dynamic values in Spring Context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamic values in Spring Context

    I'm outputing data to a file using an XML Writer in SpringBatch. it needs me to inject a file into it. What I'd like is for that file to have a timestamp, like so:

    Code:
    <bean id="outputFile" class="org.springframework.core.io.FileSystemResource">
      <constructor-arg value="${OUTPUT_DIR}/output.[timestamp()].xml" />
    </bean>
    Where [timestamp()] is a function or something dynamic. ${Properties} are of course possible but they are static.

    I'm not just interested in timestamps but also an answer to the general question of supplying dynamic values into spring context files

  • #2
    Remember that with dependency injection, dependencies are injected at the time the bean is created. There's no getting around that whatsoever.

    What you can do is make this bean a prototype bean instead of a singleton (and assuming whatever this bean is relying on this one can be accommodating of that) then the timestamp can be inserted using Spring's expression language.

    http://static.springsource.org/sprin...pressions.html

    This seems clunky at best though. Prototype beans are usually best to avoid.

    A better alternative may be to provide your own alternate implementation of FileSystemResource that defers the naming of the file in question until the last possible moment and will add the timestamp to the filename at that time.
    Last edited by krancour; Nov 1st, 2010, 11:53 AM.

    Comment


    • #3
      It looks like the expression language is my best bet for dynamic setting of bean properties. Sadly no date functions, but, extending the XMLWriter like you suggest would also work. Thanks, I wanted to be sure there was no better way.

      Comment


      • #4
        @martinh,

        what is the problem you are trying to solve?

        in Spring Batch, for example, that is what "late binding" is used for:

        Code:
        <bean id="flatFileItemReader" scope="step"
                   class="org.springframework.batch.item.file.FlatFileItemReader">
            <property name="resource" value="#{jobParameters['input.file.name']}" />
        </bean>
        So the job is started with parameters that can be used at run time. Notice that the reader is step scoped, and only created per new step / for duration of that step.

        /Anatoly

        Comment


        • #5
          @litius,

          > what is the problem you are trying to solve?

          2 things:

          1) If I was creating a batch job in CRON it would be easy for me to output to a file with a timestamp. So, I'm trying to find out how best to do this in Spring and Spring Batch.

          2) I'm new to Spring Batch and newish to Spring itself, I know I can do this several ways, so I'm also asking to learn the best practice.

          I didn't know about "late binding" I'll go off and read all about that too - like I did for SpEL.

          It's also good to know about 'step scope' that makes a lot of sense too.

          Thanks for the insights,
          Martin.

          Comment

          Working...
          X