0
我們使用spring來構造/注入我們的java bean。下面的代碼段:使用函數/構造函數參數創建單例(用於例如注入)
<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");
由於豆僅讀了一些性能,這是我總是懷疑可能有問題一樣。但我仍然很好奇線程安全的方式來實現它 當然有線程安全的Double Checked Locking with usage of volatile。 但是有沒有另外一種方法可能使用Initialization on demand holder idiom與函數/構造函數參數?
這與JSF有什麼關係? – BalusC