如何使嵌入式 Jetty 使用它定义为 servlet 的父上下文的 AppContext [英] How to make embedded Jetty use the AppContext it's defined in as parent context for servlets

查看:42
本文介绍了如何使嵌入式 Jetty 使用它定义为 servlet 的父上下文的 AppContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个独立的 Java 应用程序,它现在运行一个嵌入式 Jetty 服务器来为 HTTP 公开一个 RESTful API.从 Hibernate 到 Jetty,它确实大量使用 Spring bean.我为 Jetty 配置了一个 DispatcherServlet(我的想法是在未来添加一个非 REST API 就像制作新的控制器并为调度程序正确映射它一样简单).

I have a standalone java app that now runs an embedded Jetty server to expose a RESTful API for HTTP. It does make heavy use of Spring beans for everything from Hibernate to Jetty. I have Jetty configured with a DispatcherServlet ( the thought being that adding a non-REST API in the future will be as simple as making the new Controller and mapping it correctly for the dispatcher).

我的应用程序有一个带有 main 方法的类,该类从我的 appContext.xml 创建一个 ClassPathXmlApplicationContext 来启动一切.

My app has a class with a main method that creates a ClassPathXmlApplicationContext from my appContext.xml to start everything up.

ApplicationContext ac= new ClassPathXmlApplicationContext(new String[] { "appContext.xml" });

我不知道如何让在 DispatcherServlet 的上下文配置文件中定义的 bean 能够访问在定义 jetty 的 appContext.xml 中定义的 bean.我的 Jetty 定义如下所示:

I don't know how to make beans defined in the context config file for the DispatcherServlet have access to beans defined in the appContext.xml where jetty is defined. My Jetty definition looks like this:

<bean id="JettyServer" class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="stop">

    <constructor-arg>
            <bean id="threadPool" class="org.eclipse.jetty.util.thread.QueuedThreadPool">
                <property name="minThreads" value="2"/>
                <property name="maxThreads" value="10"/>
            </bean>
    </constructor-arg>

    <property name="connectors">
        <list>
            <bean id="Connector" class="org.eclipse.jetty.server.ServerConnector">
                <constructor-arg ref="JettyServer"/>
                <property name="port" value="8090"/>
            </bean>
        </list>
    </property>

    <property name="handler">
        <bean id="handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
            <property name="handlers">
                <list>
                    <bean class="org.eclipse.jetty.servlet.ServletContextHandler">
                        <property name="contextPath" value="/"/>
                        <property name="servletHandler">
                            <bean class="org.eclipse.jetty.servlet.ServletHandler">
                                <property name="servlets">
                                    <list>
                                        <bean class="org.eclipse.jetty.servlet.ServletHolder">
                                            <property name="name" value="DefaultServlet"/>
                                            <property name="servlet">
                                                <bean class="org.springframework.web.servlet.DispatcherServlet"/>
                                            </property>
                                            <property name="initParameters">
                                                <map>
                                                    <entry key="contextConfigLocation" value="classpath:./DefaultServlet.xml" />
                                                </map>
                                            </property>
                                        </bean>
                                    </list>
                                </property>
                                <property name="servletMappings">
                                    <list>
                                        <bean class="org.eclipse.jetty.servlet.ServletMapping">
                                            <property name="pathSpecs">
                                                <list><value>/</value></list>
                                            </property>
                                            <property name="servletName" value="DefaultServlet"/>
                                        </bean>
                                    </list>
                                </property>
                            </bean>
                        </property>
                    </bean>
                    <bean class="org.eclipse.jetty.server.handler.RequestLogHandler">
                        <property name="requestLog">
                            <bean class="org.eclipse.jetty.server.NCSARequestLog">
                                <constructor-arg value="/opt/impulse/logs/jetty-yyyy_mm_dd.log"/>
                                <property name="extended" value="false" />
                            </bean>
                        </property>
                    </bean>
                </list>
            </property>
        </bean>
    </property>

  </bean>

然后在 DefaultServlet.xml 中,我尝试定义一个 bean,该 bean 的属性引用了在 appContext.xml 中定义的 bean,这就是中断.

Then in DefaultServlet.xml I try to defined a bean with a property references a bean defined in appContext.xml, which is what breaks.

<bean id="restApiController" class="com.mycompany.myapp.api.controllers.RESTfulController">
    <property name="someBean" ref="someBean"/>
</bean>

推荐答案

您正在使用 applicationContext.xml 引导 Jetty,这反过来又使用您的 servlet 配置设置了 jetty.在其中,您正在使用指向 servlet 应用程序上下文的 contextConfigLocation 参数配置 servlet.即使您嵌入它,它仍将作为 web 应用程序运行.因此,您还需要为您的 servlet 提供其他 bean 的配置.我建议您将码头设置提取到它自己的文件中,然后将其余的 bean 提取到另一个文件中.然后在 contextConfigLocation 中提供另一个上下文文件.

You are bootstrapping Jetty with applicationContext.xml, which in turn sets up jetty with your servlet configuration. Inside it you are configuring your servlet with the contextConfigLocation parameter pointing to the servlet application context. It will still run as a webapp, even if you embed it. So you need to provide your servlet with the config to your other beans as well. I suggest that you extract the jetty setup into it's own file, and then the rest of your beans in a different file. You then supply the other context file in the contextConfigLocation.

编辑

如果你真的需要在jetty和你的servlet之间共享应用上下文,也许你可以使用这个博客.这似乎是可能的,但看起来您必须手动连接上下文之间的父/子关系.

If you really need to share the application context between jetty and your servlet, maybe you can use some of the information in this blog. It seems to be possible, but it looks like you have to wire up the parent/child relationship between the contexts manually.

这篇关于如何使嵌入式 Jetty 使用它定义为 servlet 的父上下文的 AppContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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