我正在构建一个 Spring 应用程序(它是在 Geronimo 服务器上运行的单例应用程序,而不是 Web 服务或 MVC 应用程序)。在其负载上,我尝试访问内部应用程序上下文(它将管理一组原型(prototype) bean - 基本上尝试使用分层应用程序上下文来管理所有原型(prototype) bean,以彻底关闭这些 bean)。
我研究了以下各种方法:
基本上创建一个辅助 servlet(对我来说看起来不是最好的选择)并访问它们
或者使用 GenericWebApplicationContext(也尝试了所有其他与 WebApplicationContext 相关的 API),如下所示:
GenericWebApplicationContext context = new GenericWebApplicationContext(servletContext);
context.setParent(rootApplicationContext);
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
xmlReader.loadBeanDefinitions(new ClassPathResource("ApplicationContext/beans.xml"));
context.refresh();
请对这些方法发表评论。有什么建议的方法吗?
此外,我还收到以下错误:
2013-12-13 00:44:04,877 ERROR [ContextLoader] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ZKWatcherImpl' defined in URL [bundleresource://382.fwk1189431013/com/ebay/traffic/email/aggregate/watcher/ZKWatcherImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ebay.traffic.email.aggregate.watcher.ZKWatcherImpl]: Constructor threw exception;
nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context] Offending resource: class path resource [CustomContext.xml]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
谢谢
最佳答案
在异常方面,您实际上没有正确导入自定义命名空间。 dtd 不存在,或者您没有引用 CustomContext.xml 中的正确公共(public)位置。
关于您的主要问题,为什么您需要第二个 servlrt 或 WebApplicationContext 本身。你需要管理原型(prototype)bean 有很多这样的问题
Spring: How to cleanly terminate prototype-scoped beans?
首先根据您的适当要求搜索SO。你会得到很多。
关于java - 创建自定义 Web 应用程序上下文 [SPRING],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20562662/