복붙노트

[SPRING] 스프링 빈이 CXF 웹 서비스에 주입되지 않는다. 이유는 무엇인가?

SPRING

스프링 빈이 CXF 웹 서비스에 주입되지 않는다. 이유는 무엇인가?

Spring (Autowired)을 사용하여 다른 클래스를 주입하는 RESTful 서비스 (JBoss에서 CXF 사용)를 작성 중입니다. 그러나 클래스는 주입되지 않고 null입니다.

웹 서비스 인터페이스 및 클래스 (주입이 필요한 곳)

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>

My service is active (http : // localhost : 8080 / {warname} / myws / doSomething) MyCore 인스턴스가 myCore 필드에 MyWebService로 주입되지 않습니다. 항상 null이며 내 서비스가 예상대로 작동하지 않고 대신 NullPointerException을 throw합니다.

Google을 통해 수집 된 모든 의견을 시도했습니다. 불운! 귀하의 도움은 높이 평가됩니다.

문안 인사

해결법

  1. ==============================

    1.웹 서비스에 아래 메소드를 추가하십시오.

    웹 서비스에 아래 메소드를 추가하십시오.

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

    현재 웹 응용 프로그램 컨텍스트 (일반적으로 ContextLoaderListener에 의해로드 된 컨텍스트)는 자동 와이어 링에 사용되므로 IMyCore Bean은 웹 서비스가 아닌 컨텍스트 수신기 구성 파일에 정의되어야합니다.

  2. ==============================

    2.CXF 웹 서비스 클래스에서 Spring Beans를 사용하려면 WebService를 CXF의 XML 구성 파일 (예 : spring-cxf.xml)에서 다음과 같이 선언하십시오.

    CXF 웹 서비스 클래스에서 Spring Beans를 사용하려면 WebService를 CXF의 XML 구성 파일 (예 : spring-cxf.xml)에서 다음과 같이 선언하십시오.

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

    WebService 클래스에 대해 분리 된 bean을 선언 한 다음 ID가있는 엔드 포인트에 넣으십시오. 이렇게하면 Spring 관리 bean이 생겨서 AutoWired 어노테이션을 사용할 수있다.

    다음과 같이 웹 서비스를 선언 할 경우 빈은 자동으로 주입되지 않습니다.

    <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"/>
    

    이 경우 다음 중 하나가 필요합니다.

  3. ==============================

    3.Beans.xml 아래에 Bean 설정을 추가해보십시오.

    Beans.xml 아래에 Bean 설정을 추가해보십시오.

    <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
    

    내 경우에는 ..

  4. from https://stackoverflow.com/questions/10456119/spring-bean-not-injected-into-cxf-web-service-why by cc-by-sa and MIT license