首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用spring配置karaf中的karaf服务

用spring配置karaf中的karaf服务
EN

Stack Overflow用户
提问于 2015-03-09 13:13:27
回答 1查看 139关注 0票数 1

与环境有关:

  • 卡拉夫3.0.1
  • 春季3.2.4
  • 哈斯式4.0.33

我已经通过CXF公开了一个服务,现在我试图公开与Hessian服务相同的服务。

没有war或web.xml,只有普通bean+pax,我尝试了以下方法:

代码语言:javascript
复制
<bean name="/hessian" class="org.springframework.remoting.caucho.HessianServiceExporter">
    <property name="service" ref="promocionalOnLineWebServiceBean"/>
    <property name="serviceInterface" value="org.fideliapos.promos.webservice.PromocionalOnLineFacade"/>
</bean> 
...
<bean id="hessianServlet" class="org.springframework.web.context.support.HttpRequestHandlerServlet"/>
...
<osgi:service ref="hessianServlet" interface="javax.servlet.http.HttpServlet">
    <service-properties>
        <entry key="alias" value="/hessian"/>
    </service-properties>         
</osgi:service>

这个想法是注册一个servlet (一个HttpRequestHandlerServlet),它的目标是一个HessianServiceExporter,但是我得到了一个,no,WebApplicationContext,found: no ContextLoaderListener注册?

我已经跟踪了spring代码,内部jetty正在识别servlet并调用其init方法:

代码语言:javascript
复制
@Override
public void init() throws ServletException {
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
    this.target = wac.getBean(getServletName(), HttpRequestHandler.class);
}

这就是问题所在,因为没有spring WebApplicationContext,目标属性也不能被忽略。

我是不是遗漏了什么?或者不可能让它像这样工作。

作为一种解决方法,我正在考虑用我自己的实现(setTarget等等)扩展Servlet,但我不想这么做。

更新

在尝试创建和添加我自己的HttpContext之后,仍然缺少一些东西:

我实现了自己的HttpContext:

代码语言:javascript
复制
public class HessianContext implements HttpContext{
...
}

添加豆子

代码语言:javascript
复制
<bean id="hessianContext" class="org.fideliapos.promos.hessian.HessianContext"/>

服务:

代码语言:javascript
复制
<osgi:service id="hessianContextService" ref="hessianContext" interface="org.osgi.service.http.HttpContext">
    <service-properties>
        <entry key="httpContext.id" value="hessian"/> <!-- also tried with contextId-->
    </service-properties>
</osgi:service>     

最后,servlet作为服务:

代码语言:javascript
复制
<osgi:service ref="hessianServlet" interface="javax.servlet.http.HttpServlet">
    <service-properties>
        <entry key="alias" value="/hessian"/>
        <entry key="httpContext.id" value="hessian"/> <!-- also tried with contextId-->      
    </service-properties>         
</osgi:service>

由于init方法正在寻找一个WebApplicationContext,所以看起来我应该声明和显式的GenericWebApplicationContext bean,但是我不知道如何将这个bean与OSGi所需的HttpContext‘连接’。

EN

回答 1

Stack Overflow用户

发布于 2015-03-10 07:40:04

看起来您需要将servlet添加到用于servlet的WebApplicationContext中。现在您使用Pax的DefaultHttpContext。在您的例子中,您需要注册一个自定义的HttpContext,它知道Spring的内容,这样这个HttpContext就能够提取这些信息。为此,您需要将您的自定义HttpContext注册为服务并在Servlet中引用它,使用蓝图(类似于spring)的完整示例可以找到这里

以下是本报告的摘录:

代码语言:javascript
复制
<service id="forbiddenCtxtService" ref="forbiddenContext" interface="org.osgi.service.http.HttpContext">
    <service-properties>
        <entry key="httpContext.id" value="forbidden"/>
    </service-properties>
</service>

重要的部分是httpContext.id

代码语言:javascript
复制
<bean id="forbiddenServlet" class="org.ops4j.pax.web.extender.samples.whiteboard.internal.WhiteboardServlet">
    <argument type="java.lang.String" value="/forbidden"/>
</bean>

<service id="forbiddenServletService" ref="forbiddenServlet" interface="javax.servlet.Servlet">
    <service-properties>
        <entry key="alias" value="/forbidden"/>
        <entry key="httpContext.id" value="forbidden"/>
    </service-properties>
</service>

在这里,注册的Servlet确实有相应的httpContext.id的配置,这将这个Servlet绑定到以前注册的HttpContext。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28942887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档