论坛首页 入门技术论坛

如何在spring项目里用jndi方式获得beanfactory

浏览 2932 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-01-20  
  题目说的有点不太清楚,讲讲实际的问题吧。
  我使用spring在web.xml中使用了
  org.springframework.web.context.ContextLoaderListener
  装入了所有的bean实例可以正常使用。
  但是现在程序里有一个要求可以动态的装入各个服务bean,所有要获得beanfactory才能得到。我在想spring使用contextloaderlistener时,是否会将装入的beanfactory挂到jndi树上呢,还是怎么获得,请各位!谢谢
  排除自己重新new一个beanfactory实例,那是重复的加载。。
   发表时间: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());
}
}

}
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics