确保从非spring上下文加载spring bean [英] Ensure spring bean loaded from non spring context

查看:226
本文介绍了确保从非spring上下文加载spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在(jersey 2.6类和)Servlet旁边有spring应用程序.

I have spring application alongside (jersey 2.6 classes and ) servlets .

我需要从 jersey/非spring上下文中获取Spring bean,

I need to get Spring bean(s) from jersey/non spring context,

类似问题建议在上下文的静态包装中获取上下文

Similar question suggested to get context in a static wrapper of context

public static ApplicationContext getContext() {
    return context;
}

如何确定上下文已加载或不为null?

How can I be sure the context is already loaded or not null?

如果不能,我应该如何等待/检查它是否已加载春天上下文?

If I can't, how should I wait/check until it spring context is loaded?

如果从 jersey上下文调用或从调用Bean,则使用简单的HttpServlet代码

In case of calling from jersey context or calling bean from a simple HttpServlet code

编辑

Jersey使用jersey-spring3依赖项jar可以正常工作,所以我的问题只是关于Spring控制范围之外的Servlets

Jersey is working fine using jersey-spring3 dependency jar, so my question is only about Servlets out of Spring control

编辑2

应用程序正在加载的弹簧不同于@entpnerd建议的文章

The application is loading spring different than @entpnerd suggested article

它注册一个实现WebApplicationInitializer

public class MyWebAppInitializer implements WebApplicationInitializer {

而且还在web.xml中配置了DispatcherServlet

But also have DispatcherServlet configured in web.xml

如何仅在Spring加载后才能加载DispatcherServlet?

How can the DispatcherServlet loaded only after Spring loaded?

因为我们在其init方法上添加了自动装配功能:

Because we add Autowiring capabilities on its init method:

WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext())
                    .getAutowireCapableBeanFactory().autowireBean(this);

最喜欢的解决方案是在服务请求之前添加超时,还是可以在类加载中进行一些调整?

Is adding a timeout before serving requests is the most prefer solution or is there a tweak in class loading that can take care of it?

编辑3

我找到了答案

I found answers and answers of injecting, but not why Spring is loaded before Servlet.

推荐答案

这个想法很简单,尽管实际实现可能会因Spring Boot和Jersery初始化的确切方式而异.

The idea is quite simple, although the actual implementation may vary depending on an exact way of Spring boot and Jersery initialization.

一个想法:

Spring Boot作为纯粹的运行时框架,是关于正确加载应用程序上下文的(从问题的角度来看).

Spring boot, as being a purely runtime framework, is all about proper loading the application context (from the question standpoint).

因此,最重要的是,在加载时,在内存中的某个位置存在一个应用程序上下文,并且可以从该应用程序上下文访问bean.

So, bottom line, when it's loaded there is an application context somewhere in memory, and its possible to access beans from this application context.

现在,由于您说Jersey不是由spring/spring-boot驱动的,因此必须通过Jersey的某种静态全局变量可以访问此应用程序上下文,这很丑陋,但应该可以使用.

Now, since you say that Jersey is not spring/spring-boot driven, this application context has to be reachable from some kind of static global variable by Jersey, it's quite ugly but should work.

所以这个想法有两个步骤:

So the idea has two steps:

  1. 将应用程序上下文引用放置到可从Jersey访问的某些静态持有人.
  2. 从Jersey组件的一些基础结构级别代码中读取此值.

可能的实施方式

从技术上讲,<第一步>第一步可以通过实现某种形式的Spring Boot侦听器来完成,该侦听器将以某种单例形式存储应用程序上下文:

Technically step one can be done by implementing some kind of spring boot listener that will store application context in some kind of singleton:

enum ApplicationContextHolder {
   INSTANCE;
    private ApplicationContext ctx;
    void setApplicationContext(ApplicationContext ctx) {
        this.ctx = ctx;
    }

    ApplicationContext getCtx() {
        return this.ctx;
    }

}


// and a listener (spring boot provides many ways to register one, but the 
// implementation should be something like this):
// The main point is that its managed by spring boot, and hence and access to 
// the application context
class StartupListener implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {           
             ApplicationContextHolder
             .INSTANCE
             .setApplicationContext(event.getApplicationContext());
    }
}

现在第2步是:

class MyJerseyOrWhateverComponentThatWantsToAccessApplicationContext {

    public void foo() {
       ApplicationContext ctx = ApplicationContextHolder.INSTANCE.getCtx();
       ... 
       ctx.getBean(...);
    }
}

这篇关于确保从非spring上下文加载spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆