简体   繁体   English

如何获得Spring上下文

[英]How to get Spring Context

I have a third party library that starting up my aplication, load spring context and save it in private static filed without any acessors. 我有一个第三方库来启动我的应用程序,加载spring上下文并将其保存在没有任何acessor的私有静态文件中。

I need to get this context to my application. 我需要将此上下文添加到我的应用程序中。 Does spring provides some callbacks like afterContextLoaded methods or any other ways how to get it? spring是否提供了一些回调,例如afterContextLoaded方法或其他任何获取方法?

Reflection is not a solution for me! 反思不是我的解决方案!

Thanks. 谢谢。

you have several ways to archive that: 您可以通过以下几种方式进行归档:

  1. Use ApplicationListerner and ApplicatinEvent 使用ApplicationListerner和ApplicatinEvent
  2. Use ApplicationContextAware 使用ApplicationContextAware

Both of them can be found at http://static.springsource.org/spring/docs/3.0.0.M4/reference/html/ch03s13.html (sorry for the link is not to the latest version) 两者都可以在http://static.springsource.org/spring/docs/3.0.0.M4/reference/html/ch03s13.html上找到(很抱歉,链接不是最新版本)

Just implement ApplicationContextAware in a Spring bean. 只需在Spring bean中实现ApplicationContextAware

If you want to access the AppContext on startup, then also implement InitializingBean and put your code in afterPropertiesSet() - the ApplicationContext instance will be populated and ready for use at that time. 如果要在启动时访问AppContext,则还应实现InitializingBean并将代码放入afterPropertiesSet()中 -此时将填充ApplicationContext实例并准备使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spring 如何从应用程序上下文中获取 BeanFactory - spring how to get BeanFactory from Application Context 在spring mvc中如何获取控制器中的上下文路径 - In spring mvc how to get the context path in controller 如何从 maven 模块获取 spring 上下文 - How to get spring context from maven module 如何在反应式 webflux 中获取 spring 安全上下文 - How to get spring security context in reactive webflux 如何将tomcat context.xml参数值获取到Spring PropertyPlaceholderConfigurer中 - How to get tomcat context.xml Parameters values into spring PropertyPlaceholderConfigurer Spring:如何在Webapp和Standalone程序中获取Application上下文 - Spring: how to get hold of Application context in Webapp and Standalone program 如何在 Spring Boot 中使用应用程序上下文获取 bean - How to get bean using application context in spring boot 如何在 Junit 5 *before* Spring 测试上下文加载之前获取回调? - How to get callback in Junit 5 *before* Spring test context is loaded? 如何从Freemarker宏的上下文中获取Spring bean? - How to get spring bean from context in freemarker macros? 如何在Spring MVC + Maven项目中获取Web上下文路径? - how to get web context path in spring mvc + maven project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM