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

Viv*_*han 9 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) {
      ....
    }
}
Run Code Online (Sandbox Code Playgroud)

那必须注入

package com.company.project.biz;

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

@Component("myCore")
public class MyCore implements IMyCore {
    public MyResponse doSomething(MyRequest myRequest) {
            .....
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

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

问候

小智 8

尝试将以下方法添加到您的Web服务:

@PostConstruct
public void init() {
    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
Run Code Online (Sandbox Code Playgroud)

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


X-H*_*Man 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" />
Run Code Online (Sandbox Code Playgroud)

为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"/>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您将需要:

  • 手动注入弹簧豆

    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

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

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

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

    来自CXF官方文档.


asp*_*t75 1

尝试在 Beans.xml 添加以下 bean 配置

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
Run Code Online (Sandbox Code Playgroud)

就我而言,它有效..