未为 JAX-RS CXF 服务注入带注释的 bean

发布于 2024-10-09 21:14:30 字数 1308 浏览 0 评论 0原文

我有一个 CXF RESTful 服务,运行得很好。问题是当我尝试使用注释注入 DAO 时:

@Resource
private MyDAO myDAO;

它没有被注入。我的 JAX-RS 服务是 Spring 配置的,如下所示:

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:jaxws="http://cxf.apache.org/jaxws"
      xmlns:jaxrs="http://cxf.apache.org/jaxrs"
      xmlns:cxf="http://cxf.apache.org/core"
      xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">


  <import resource="classpath:META-INF/cxf/cxf.xml"/>
  <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

  <jaxrs:server id="message" serviceClass="com.foo.MessageResourceImpl" address="/">
    <jaxrs:features>
         <cxf:logging/>
    </jaxrs:features>
  </jaxrs:server>
</beans>

DAO 正在由 Spring 初始化,并且我已经验证了该 bean 在任何其他 POJO 中都有效,只是在 CXF 服务中无效。此外,我在日志中没有看到任何错误

I have a CXF RESTful service which is working just fine. Problem is when I try to inject my DAO using an annotation:

@Resource
private MyDAO myDAO;

It's not getting injected. My JAX-RS service is Spring configured like so:

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:jaxws="http://cxf.apache.org/jaxws"
      xmlns:jaxrs="http://cxf.apache.org/jaxrs"
      xmlns:cxf="http://cxf.apache.org/core"
      xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">


  <import resource="classpath:META-INF/cxf/cxf.xml"/>
  <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

  <jaxrs:server id="message" serviceClass="com.foo.MessageResourceImpl" address="/">
    <jaxrs:features>
         <cxf:logging/>
    </jaxrs:features>
  </jaxrs:server>
</beans>

The DAO is getting initialized by Spring and I have verified the bean works in any other POJO, just not the CXF service. Furthermore, I don't see any errors in the logs

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

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

发布评论

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

评论(1

嗼ふ静 2024-10-16 21:14:30

解决方案是serviceBeans:

<jaxrs:serviceBeans>
    <bean class="com.foo.MessageResourceImpl"/>
</jaxrs:serviceBeans>

当使用@serviceClass时,CXF实例化该类,而不是Spring。

The solution is to serviceBeans:

<jaxrs:serviceBeans>
    <bean class="com.foo.MessageResourceImpl"/>
</jaxrs:serviceBeans>

When using @serviceClass, CXF instantiates the class, not Spring.

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