如何在spring项目里用jndi方式获得beanfactory
题目说的有点不太清楚,讲讲实际的问题吧。
我使用spring在web.xml中使用了
org.springframework.web.context.ContextLoaderListener
装入了所有的bean实例可以正常使用。
但是现在程序里有一个要求可以动态的装入各个服务bean,所有要获得beanfactory才能得到。我在想spring使用contextloaderlistener时,是否会将装入的beanfactory挂到jndi树上呢,还是怎么获得,请各位!谢谢
排除自己重新new一个beanfactory实例,那是重复的加载。。
1 楼 cljhyjs 2007-08-07 解决方法如下:
public class SpringInitListener implements javax.servlet.ServletContextListener {
private ContextLoader contextLoader;
/**
* Initialize the root web application context.
*/
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
WebApplicationContext context = this.contextLoader
.initWebApplicationContext(event.getServletContext());
SpringBeanFactory.setAppContext(context);
}
/**
* Create the ContextLoader to use. Can be overridden in subclasses.
*
* @return the new ContextLoader
*/
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
/**
* Return the ContextLoader used by this listener.
*/
public ContextLoader getContextLoader() {
return contextLoader;
}
/**
* Close the root web application context.
*/
public void contextDestroyed(ServletContextEvent event) {
if (this.contextLoader != null) {
this.contextLoader.closeWebApplicationContext(event
.getServletContext());
}
}
}