如何将Spring Application Context事件与其他上下文联系起来

我有一个带有两个上下文的Spring Web应用程序:一个( applicationContext )由ContextLoaderListener构建,另一个( webContext )由DispatcherServlet构建。

applicationContext是一个bean( org.springframework.security.authentication.DefaultAuthenticationEventPublisher ),用于触发spring上下文事件。

但事件的接收者是在webContext定义的。 那个接收器没有得到这个事件。 (如果将接收器作为测试目的放在applicationContext那么它就会得到事件,但我不能这样做,因为我需要webContext来实现它的function。)

所以我的问题是,如何将事件从applicationContext桥接到webContext

我有同样的问题,通过将创建事件的bean移动到web上下文来解决我的问题。 但是,您可以通过手动连接事件监听器来解决您的问题,这样的事情(此代码未编译,因此未经测试):

 @Component public class BeanInWebContext implements ApplicationListener { @Autowired private ApplicationContext webContext; @PostConstruct public void registerAsListener() { // get parent context AbstractApplicationContext appContext = (AbstractApplicationContext) webContext.getParent(); // register self as a listener, this method is in AbstractApplicationContext appContext.addApplicationListener(this); } @Override public void onApplicationEvent(SomeEvent event) { } } 

我认为实际的答案是你可能想要以不同的方式配置你的应用程序(这样你只有一个上下文)我认为在你的web.xml中你需要做这样的事情:

  example org.springframework.web.servlet.DispatcherServlet 1  contextConfigLocation  classpath:/META-INF/applicationSpringConfig.xml    

但要回答更深层次的问题。 其他人指出你可以在spring文件中使用includes(事实上在上面你可以在你的调度程序servlet中指定多个springconfig)。 但是,当您包含其他上下文文件时,您不会共享bean的实例,只能共享定义。

与EJB等相比,模块化Spring应用程序一直是Spring的唯一真正缺点。这导致了使用OSGi。 关于如何共享spring上下文的基本问题的答案,正式你使用OSGi(spring dm)在上下文之间共享spring bean实例

尝试将事件发布者移动到Web上下文文件,在该文件中应该可以看到整个应用程序上下文。 在父应用程序上下文中配置方法安全性时会发生类似的问题。 父应用程序上下文(由ContextLoaderListener加载)不知道子(Web)上下文。

如果您不需要两者之间的父子关系,也可以为整个应用程序使用单个应用程序上下文。 通常它只是妨碍了,如果在同一个空间中定义所有bean,则更容易。

正如spring框架的文档中所述,简单的ApplicationEvent机制仅设计用于同一应用程序上下文中,我不知道可以将事件传播到子上下文。

如果您需要更高级的解决方案,可以考虑使用更强大的解决方案,如Java Message Service或Spring Integration。

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#context-functionality-events

我们可以使用import标签导入/桥接以事件/ bean的可见性可用和共享的方式创建的两个不同的上下文。

  

在此导入中,上下文xml配置为在DispatcherServlet的上下文xml中从ContextLoaderListener创建。