我有這樣的單例類:春季和IOC對我的Singleton類(web應用程序,Tomcat)的
package ua.org.bytes.ewt;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import ua.org.bytes.ewt.dao.CategoriesManager;
import ua.org.bytes.ewt.dao.ShortcutsManager;
/**
*
* @author Vitaliy Ganzha
*/
public class ApplicationSupervisor {
private static final ApplicationSupervisor INSTANCE = new ApplicationSupervisor();
private CategoriesManager categoriesManager;
public void setCategoriesManager(CategoriesManager categoriesManager) {
this.categoriesManager = categoriesManager;
}
public void setShortcutsManager(ShortcutsManager shortcutsManager) {
this.shortcutsManager = shortcutsManager;
}
private ShortcutsManager shortcutsManager;
public ShortcutsManager getShortcutsManager() {
return shortcutsManager;
}
private ApplicationSupervisor(){
/*WebApplicationContext context =
WebApplicationContextUtils.getRequiredWebApplicationContext(
ServletActionContext.getServletContext()
);
categoriesManager = (CategoriesManager)context.getBean("categoriesManager");
shortcutsManager = (ShortcutsManager)context.getBean("categoriesManager");*/
}
public static ApplicationSupervisor getInstance(){
return INSTANCE;
}
/**
* @return the categoriesManager
*/
public CategoriesManager getCategoriesManager() {
return categoriesManager;
}
}
而且applicationContext.xml中:
<beans default-autowire="autodetect">
<bean id="categoriesManager" class="ua.org.bytes.ewt.dao.CategoriesManagerImpl"></bean>
<bean id="shortcutsManager" class="ua.org.bytes.ewt.dao.ShortcutsManagerImpl"></bean>
<bean id="applicationSupervisor" class="ua.org.bytes.ewt.ApplicationSupervisor" singleton="true">
<property name="categoriesManager">
<ref local="categoriesManager"/>
</property>
<property name="shortcutsManager">
<ref local="shortcutsManager"/>
</property>
</bean>
</beans>
所以,我有一個類所有實體經理。我想用Spring IoC設置Interceptor CategoriesManager和ShortcutsManager上的所有實現。 我已經添加到聽者的web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
但是當我嘗試與他們合作,我得到NullPointerException異常: ApplicationSupervisor.getInstance()getShortcutsManager(); =會返回null。
請幫助我,我是Spring IoC的新成員。
謝謝!
我想將ApplicationSupervisor注入到bean中,但我拒絕了這個想法,因爲我有很多bean,也許我會有一個插件系統,它會使用這些對象。 – vmg 2010-11-04 12:13:14
*不是說singleton =「true」在這裏是多餘的*我想你的意思是*請注意...... * – 2010-11-04 13:13:46