如何将 Jetty 嵌入 Spring 并使其使用嵌入的相同 AppContext?

发布于 2024-09-07 18:41:30 字数 264 浏览 3 评论 0原文

我有一个 Spring ApplicationContext,在其中声明 Jetty 服务器 bean 并启动它。在 Jetty 内部,我有一个 DispatcherServlet 和几个控制器。如何使 DispatcherServlet 及其控制器使用声明 Jetty 的同一 ApplicationContext 中的 bean?

事实上,在外部上下文中,我有几个类似守护进程的 bean 及其依赖项。 Jetty 内部的控制器使用相同的依赖项,因此我想避免在 Jetty 内部和外部重复它们。

I have a Spring ApplicationContext where I declare Jetty server bean and start it. Inside Jetty I have a DispatcherServlet and a couple of controllers. How to make that DispatcherServlet and its controllers use beans from the same ApplicationContext where Jetty is declared?

In fact, in that outer context I have a couple of daemon-like beans and their dependencies. Controllers inside Jetty use the same dependencies, so I'd like to avoid duplicating them inside and outside Jetty.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哭泣的笑容 2024-09-14 18:41:30

我不久前做过这个。

Spring 的文档建议您使用一个 ContextLoaderListener 来加载 servlet 的应用程序上下文。使用您自己的侦听器来代替这个 Spring 类。这里的关键是你的自定义监听器可以在 Spring 配置中定义,并且可以知道它定义的应用程序上下文;因此,它不加载新的应用程序上下文,而是返回该上下文。

侦听器看起来像这样:

public class CustomContextLoaderListener extends ContextLoaderListener implements BeanFactoryAware {

    @Override
    protected ContextLoader createContextLoader() {
        return new DelegatingContextLoader(beanFactory);
    }

    protected BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
       this.beanFactory = beanFactory;
    }

}

DelegatingContextLoader 执行以下操作:

public class DelegatingContextLoader extends ContextLoader {

    protected BeanFactory beanFactory;

    public DelegatingContextLoader(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    @Override
    protected WebApplicationContext createWebApplicationContext(ServletContext servletContext, ApplicationContext parent) throws BeansException {
        return new GenericWebApplicationContext((DefaultListableBeanFactory) beanFactory);
    }

}

它有点混乱,可能可以改进,但这对我来说确实有用。

I did this a while ago.

Spring's documentation suggests that you use a ContextLoaderListener to load the application context for servlets. Instead of this Spring class, use your own listener. The key thing here is that your custom listener can be defined in the Spring config, and can be aware of the application context it's defined in; so instead of loading a new application context, it just returns that context.

The listener would look something like this:

public class CustomContextLoaderListener extends ContextLoaderListener implements BeanFactoryAware {

    @Override
    protected ContextLoader createContextLoader() {
        return new DelegatingContextLoader(beanFactory);
    }

    protected BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
       this.beanFactory = beanFactory;
    }

}

and the DelegatingContextLoader does this:

public class DelegatingContextLoader extends ContextLoader {

    protected BeanFactory beanFactory;

    public DelegatingContextLoader(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    @Override
    protected WebApplicationContext createWebApplicationContext(ServletContext servletContext, ApplicationContext parent) throws BeansException {
        return new GenericWebApplicationContext((DefaultListableBeanFactory) beanFactory);
    }

}

It's a bit messy, and can probably be improved, but this did work for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文