Spring bean没有注入CXF Web服务,为什么? [英] Spring bean not injected into CXF web service, Why?

查看:95
本文介绍了Spring bean没有注入CXF Web服务,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写RESTful服务(在JBoss上使用CXF),在其中使用Spring(自动装配)注入了另一个类.但是该类没有被注入并且为空.

I am writing a RESTful service (using CXF on JBoss) in which I have inject another class using Spring (Autowired). But the class is not getting injected and is null.

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

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

My service is active (http://localhost:8080/{warname}/myws/doSomething) but the MyCore instance is not being injected into MyWebService (in the myCore field). It is always null and my service does not work as expected, instead throws NullPointerException

对通过Google收集的所有输入进行了尝试.没运气!非常感谢您的帮助.

Tried all inputs gathered over google. No luck! Your help is highly appreciated.

致谢

推荐答案

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

Try to add below method to your web service:

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

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

The current web application context (usually the one loaded by ContextLoaderListener) will be used for autowiring, so the IMyCore bean has to be defined in the context listener configuration file and not in the web service one.

这篇关于Spring bean没有注入CXF Web服务,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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