BeanUtils 中转换器的注册是线程本地的吗?
Are register of Converters in BeanUtils thread-local?
我有一个 Web 项目,其中 BeanUtils
用于操作 bean。
在我的代码中,为了使 BeanUtils
将字符串记录正确地传输到 java.util.Date
字段中,将 DateConverter
注册到 ConvertUtils
class 中那:
ConvertUtils.register(dateConverter, Date.class);
另外,在我的项目中,不同的Action需要不同的日期格式,所以,我在不同的Action中注册了不同的转换器,比如:
public void Action1(){
DateTimeConverter dtConverter = new DateConverter();
dtConverter.setPatterns(dateFormats1);
ConvertUtils.register(dtConverter, Date.class);
...
BeanUtils.populate(myBean1, hashMap1);
}
public void Action2(){
DateTimeConverter dtConverter = new DateConverter();
dtConverter.setPatterns(dateFormats2);
ConvertUtils.register(dtConverter, Date.class);
...
BeanUtils.populate(myBean2, hashMap2);
}
但后来,我注意到具有相同目标class(此处为Date
)的已注册转换器会相互替换。所以如果ConvertUtils.register
操作不是线程本地的,并发引起的问题可能会在这里发生,即使我的网站还没有遇到过。
那么,在一个线程中注册的转换器会替换在另一个线程中注册的转换器吗?如果是这样,我的情况有什么变通办法吗?
Apache commons beanutils 使用 ContextClassLoaderLocal 来管理框架的实例。这个概念类似于 ThreadLocal
除了它将实例绑定到线程的上下文 class 加载器。
因此,当执行 Action1
和 Action2
的线程共享相同的上下文 class 加载程序时,一个操作中对 ConverterUtils
的更改将影响另一个操作。
为了安全起见,您可以在每个操作中使用自己的 BeanUtilsBean
实例,例如
public void Action1(){
BeanUtilsBean beanUtils = new BeanUtilsBean();
ConvertUtilsBean convertUtils = beanUtils.getConvertUtils();
DateTimeConverter dtConverter = new DateConverter();
dtConverter.setPatterns(dateFormats1);
convertUtils.register(dtConverter, Date.class);
...
beanUtils.populate(myBean1, hashMap1);
}
当然最好在 class 的构造函数中配置一次 BeanUtilsBean
并直接使用它。
我有一个 Web 项目,其中 BeanUtils
用于操作 bean。
在我的代码中,为了使 BeanUtils
将字符串记录正确地传输到 java.util.Date
字段中,将 DateConverter
注册到 ConvertUtils
class 中那:
ConvertUtils.register(dateConverter, Date.class);
另外,在我的项目中,不同的Action需要不同的日期格式,所以,我在不同的Action中注册了不同的转换器,比如:
public void Action1(){
DateTimeConverter dtConverter = new DateConverter();
dtConverter.setPatterns(dateFormats1);
ConvertUtils.register(dtConverter, Date.class);
...
BeanUtils.populate(myBean1, hashMap1);
}
public void Action2(){
DateTimeConverter dtConverter = new DateConverter();
dtConverter.setPatterns(dateFormats2);
ConvertUtils.register(dtConverter, Date.class);
...
BeanUtils.populate(myBean2, hashMap2);
}
但后来,我注意到具有相同目标class(此处为Date
)的已注册转换器会相互替换。所以如果ConvertUtils.register
操作不是线程本地的,并发引起的问题可能会在这里发生,即使我的网站还没有遇到过。
那么,在一个线程中注册的转换器会替换在另一个线程中注册的转换器吗?如果是这样,我的情况有什么变通办法吗?
Apache commons beanutils 使用 ContextClassLoaderLocal 来管理框架的实例。这个概念类似于 ThreadLocal
除了它将实例绑定到线程的上下文 class 加载器。
因此,当执行 Action1
和 Action2
的线程共享相同的上下文 class 加载程序时,一个操作中对 ConverterUtils
的更改将影响另一个操作。
为了安全起见,您可以在每个操作中使用自己的 BeanUtilsBean
实例,例如
public void Action1(){
BeanUtilsBean beanUtils = new BeanUtilsBean();
ConvertUtilsBean convertUtils = beanUtils.getConvertUtils();
DateTimeConverter dtConverter = new DateConverter();
dtConverter.setPatterns(dateFormats1);
convertUtils.register(dtConverter, Date.class);
...
beanUtils.populate(myBean1, hashMap1);
}
当然最好在 class 的构造函数中配置一次 BeanUtilsBean
并直接使用它。