小编Min*_*ang的帖子

如何在启动弹簧时选择性加载bean

我的问题是如何在Web应用程序的开头实现选择性加载spring bean.

背景我们的应用程序基于J2EE和Spring.我们在不同的托管服务器上运行相同的Web应用程序 在其中一些托管服务器上,我们只运行Web服务,但在其他服务器上,我们还需要运行报告,调度程序等服务.所有这些服务都在spring配置xml文件中配置为spring bean.所以我们想在启动带有Web服务的服务器时禁用一些未使用的bean.

存在的问题,我试图重写的方法customizeContextorg.springframework.web.context.ContextLoaderListener,从上下文中删除那些未使用的豆子.(我知道这不是一个好主意,删除加载的bean而不是阻止它们在第一个位置加载.那是因为我无法弄清楚如何实现它)但是,我得到了java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext.

经过一些调查,我得到的BeanFactory不能在这里使用,但我有点卡住,不知道如何实现这个功能.有人可以帮我解决这个问题吗?要么在开始时停止将bean加载到Spring中,要么在Spring启动时从bean中移除bean对我有用.

以下是我重写Method的代码customizeContext.

@Override
protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {

    super.customizeContext(servletContext, applicationContext);

    ConfigurableListableBeanFactory configurableListableBeanFactory = applicationContext.getBeanFactory();
    BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry) configurableListableBeanFactory;
    beanDefinitionRegistry.removeBeanDefinition("testBean");
}
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc

2
推荐指数
1
解决办法
7141
查看次数

标签 统计

java ×1

spring ×1

spring-mvc ×1