Inject current date string into Spring bean configuration
Well... technically you can do almost everything. I'm using FastDateFormat
because it's both fast (duh!) and thread-safe. java.text.SimpleDateFormat
can be used as well:
<bean id="fastDateFormat" class="org.apache.commons.lang.time.FastDateFormat" factory-method="getInstance">
<constructor-arg value="yyyy/MM/dd"/>
</bean>
<bean id="currentDate" class="java.util.Date" factory-bean="fastDateFormat" factory-method="format">
<constructor-arg>
<bean class="java.util.Date"/>
</constructor-arg>
</bean>
And then simply inject:
@Resource
private String currentDate; //2011/12/13
Note that it would be much simpler to run it in plain Java or using @Configuration
approach:
@Bean FastDateFormat fastDateFormat() {
return new FastDateFormat("yyyy/MM/dd");
}
@Bean String currentDate() = {
return fastDateFormat().format(new Date());
}
That being said, why don't you just write it plain Java in @PostConstruct
rather than over-relying on DI? Not everything has to be injected... The only advantage is that it makes testing easier since you can inject fake string and do not rely on current date. But in this case think of some DateProvider
interface, makes life simpler.
Also do you really want to have the same date for the whole application lifetime (it will be generated once at startup)? If not, currentDate
bean must have prototype
scope and you must lazily fetch it from the container every time you need it...
chrisbunney
Java and Python developer. Interested in software project management, agile development, quality assurance, and writing damn good code.
Updated on June 05, 2022Comments
-
chrisbunney about 2 years
I'm working on a program where I want to output to a file that has the current data in
yyy/MM/dd
format appended to the filename.I want to inject the
File
object representing the output file location into the class that needs it using Spring.However, I don't know how to append the current date to the filename argument when creating the
File
object.In actual code it's easy:
String outputFileName = "someFile"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); outputFileName += " " + sdf.format(new Date()); File outputFile = new File(outputFileName);
How can I do this in my Spring bean configuration file?
Is it even possible to do this, and if so how could I go about it?
-
chrisbunney over 12 yearsI've clarified my question, which may mean you want to update your answer? Whilst I'm still considering your actual answer, in response to your final paragraph, the program doesn't run continuously but it regularly executed and fetches CSV data from email attaches and merges it into a single file for each day. Hence the Date is set at start up and doesn't change. I guess I could just inject a String and add the date and create the file within the consuming class, but it seems cleaner to me if there's a way to pass a file in with the proper name
-
Tomasz Nurkiewicz over 12 years@chrisbunney: you can do this using Spring XML (hint:
String.concat()
,factory-bean
andfactory-method
) but I it just feels wrong. If you really want to do this, I can give you a complete solution. But I strongly advice against it. Replacing 4 lines of code with 10-20 lines of XML without any gain... Consider using@Configuration
as this at least allows you to write it in Java. But believe, it's better to write some things by hand rather than rely on DI everywhere. -
chrisbunney over 12 yearsAccepted. My actual solution was to take your advice and do the date configuration part in Java by creating a special factory method to create the correct string and initialise the bean with that and the other arguments. Not necessarily the cleanest approach, but it did the job.