Spring bean没有注入CXF Web服务,为什么?

时间:2012-05-04 21:05:26

标签: spring cxf jboss6.x

我正在编写一个RESTful服务(在JBoss上使用CXF),我在其中使用Spring(Autowired)注入另一个类。但是这个课程没有注入,而且是空的。

Web服务接口和类(需要注入的地方)

package com.company.project.web;

@Path("/myws")
public interface IMyWebService {    
   @POST
   @Path("/doSomething")    
   @Consumes("application/json")
   @Produces("application/json")
    MyResponse doSomething(MyRequest myRequest)
}

@Service("myWebService")
public class MyWebService implements IMyWebService {    
    @Autowired
    private IMyCore myCore;

    public MyResponse doSomething(MyRequest myRequest) {
      ....
    }
}

必须注入

package com.company.project.biz;

public interface IMyCore {
   MyResponse doSomething(MyRequest myRequest);
}

@Component("myCore")
public class MyCore implements IMyCore {
    public MyResponse doSomething(MyRequest myRequest) {
            .....
    }
}

的beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

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

    <context:annotation-config />
    <context:component-scan base-package="com.company.project"/>    

    <jaxrs:server id="myWebService" address="/">
        <jaxrs:serviceBeans>
            <bean class="com.company.project.web.MyWebService" />
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="json" value="application/json" />
        </jaxrs:extensionMappings>
    </jaxrs:server>
</beans>

我的服务处于活动状态(http://localhost:8080/{warname}/myws/doSomething),但MyCore实例未注入MyWebService(在myCore字段中)。它始终为null,我的服务无法按预期工作,而是抛出NullPointerException

尝试通过谷歌收集的所有输入。没运气!非常感谢您的帮助。

此致

3 个答案:

答案 0 :(得分:8)

尝试将以下方法添加到您的网络服务中:

@PostConstruct
public void init() {
    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}

当前的Web应用程序上下文(通常是ContextLoaderListener加载的上下文)将用于自动装配,因此IMyCore bean必须在上下文侦听器配置文件中定义,而不是在Web服务中定义。

答案 1 :(得分:5)

如果要在CXF Web Service类中使用Spring Beans,请在CXF的XML配置文件中将WebService声明为以下内容(例如spring-cxf.xml)

<bean id="hello" class="demo.spring.service.HelloWorldImpl" />
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />

为WebService类声明分离的bean,然后将其放在具有ID的端点中。像这样你将拥有spring managed bean,你也可以在其中使用AutoWired注释。

如果您将Web服务声明如下,那么您的bean永远不会被自动注入。

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:endpoint id="helloWorld" implementor="demo.spring.service.HelloWorldImpl" address="/HelloWorld"/>

在这种情况下,您需要:

  • 手动注入春豆

    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

  • 或者从spring上下文中逐个检索bean

    ApplicationContext context = ...; // your Spring ApplicationContext HelloBean helloBean = (HelloBean) context.getBean("bean");

    我没有为JAX-RS尝试过这个,但我认为这种方法应该是一样的。

    From CXF official documentation.

答案 2 :(得分:1)

尝试在Beans.xml

添加以下bean配置
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

就我而言,它有效..