【发布时间】:2011-07-19 11:43:06
【问题描述】:
我们使用 spring 来构造/注入我们的 java bean。这是一个sn-p:
<bean id="myAppConfigs" class="my.cool.webapp.ApplicationConfig" scope="singleton">
<constructor-arg value="8080" />
<constructor-arg value="MyAppName1" />
</bean>
我们在
中使用单例模式public static ApplicationConfig getCurrentInstance(ServletContext sctx) {
if (instance == null) {
WebApplicationContext wac = null;
if (sctx != null) {
wac = WebApplicationContextUtils.getWebApplicationContext(sctx);
}
return (ApplicationConfig) wac.getBean("myAppConfigs");
由于 bean 只读取一些始终相同的属性,我怀疑可能存在问题。但我仍然很好奇一个很好的线程安全的方法来实现它 当然有Double Checked Locking with usage of volatile 是线程安全的。 但是是否有另一种方法可以将Initialization on demand holder idiom 与函数/构造函数参数一起使用?
【问题讨论】:
-
这与 JSF 有什么关系?
标签: java spring design-patterns singleton