简体   繁体   中英

Spring 3.X Java 7 Thread Safe Date/Number Format injection

Hi guys can anyone help me out. I currently have a service class that I might have to use in a mutithreaded application can anyone help make the format classes I am using to be thread safe using spring IOC.

Here is a code snippet

@Configuration
@EnableTransactionManagement(mode=AdviceMode.ASPECTJ)
public class MyConfig
{
    ...
    @Bean
    public DateFormat dateFormatMMddyyyy()
    {
        return new SimpleDateFormat("MM/dd/yyyy");
    }
   ...
}

@Component("myServiceComponent")
public class MyServiceComponent5
{
   @Autowired 
   private DateFormat dateFormatMMddyyyy; //HEY Fix Me!!

   ...

   public void sampleProcess(Date date)
   {

   String formatedDate = dateFormatMMddyyyy.format(date);

   // do usefull stuffn here

   }
}

@Service("MyService")
{
   @Autowired
   private MyServiceComponent5 myServiceComponent5;

   public doMultiThreadProcess(Date date)
   {
      // not sure how to do this yet and probably gona ask on a different thread. =\
      // Currently more concern on how to set up MyServiceComponent5
      ...
      myServiceComponent5.sampleProcess(date);
      ...
   }

}

Can anyone comment if I am doing anything wrong. Recently I just read that the Number/DateFormat classes are not thread safe so I must either place them inside a method or use threadlocal . Can anyone help me modify my classes so I can use the second option ( The a spring way ).

By the way if you need any clarifications or additional information please just tell me. Also comments on the design will also be appreciated.

Do note the code is just a sample but it describes what I am currently doing. Also, this will be used for a desktop application if that has any bearing on the current problem.

Frameworks beeing used will be Spring 3.1, Java 7, and hibernate.

Finally, thank you in advance and code snippets will be greatly appreciated.

As far as your date formatting needs are concerned, JODA has date/time formatters that are thread safe. The library is also supported by default in Spring MVC:

With this one-line of configuation, default formatters for Numbers and Date types will be installed, including support for the @NumberFormat and @DateTimeFormat annotations. Full support for the Joda Time formatting library is also installed if Joda Time is present on the classpath.

Source

I was also able to do this using this snippet. It works hopefully this has no side effects.

@Bean()
public ThreadLocal<DateFormat> dateFormatMMddyyyy()
{
    ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>()
    {
        @Override
        protected DateFormat initialValue()
        {
            return new SimpleDateFormat("MM/dd/yyyy");
        }
    };

    return threadLocal;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM