2012-05-04 27 views
9

Tôi đang viết một dịch vụ RESTful (sử dụng CXF trên JBoss), trong đó tôi đã tiêm một lớp khác bằng Spring (Autowired). Nhưng lớp học không được tiêm và không có giá trị.Đậu xuân không được tiêm vào dịch vụ web CXF, Tại sao?

Giao diện Web Service và Class (Nơi tiêm cần phải xảy ra)

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) { 
     .... 
    } 
} 

Đó đó phải được tiêm

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> 

Tôi se rvice đang hoạt động (http://localhost:8080/{warname}/myws/doSomething) nhưng trường hợp MyCore không được đưa vào MyWebService (trong trường myCore). Nó luôn luôn là null và dịch vụ của tôi không hoạt động như mong đợi, thay vào đó ném NullPointerException

Đã thử tất cả các đầu vào được thu thập qua google. Không may mắn! giúp đỡ của bạn được đánh giá cao.

Trân

Trả lời

1

Cố gắng thêm vào dưới đây cấu hình đậu tại Beans.xml

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

Trong trường hợp của tôi, nó làm việc ..

+0

tôi đã cố gắng này và nó không làm việc cho tôi. Dường như dịch vụ web không được tạo ra bởi Spring và do đó không được autowired. –

8

Cố gắng thêm vào dưới đây phương pháp để dịch vụ web của bạn:

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

Ngữ cảnh ứng dụng web hiện tại (thường là ngữ cảnh được tải bởi ContextLoaderListen er) sẽ được sử dụng cho autowiring, vì vậy hạt IMyCore phải được định nghĩa trong tệp cấu hình trình lắng nghe ngữ cảnh và không phải trong dịch vụ web.

+0

Ngay bây giờ dự án đã thay đổi một chút để thử giải pháp bạn đã đề xuất. Nhưng tôi chắc chắn thử nó trên một bản sao cục bộ của cơ sở mã, và cho bạn mượn. Cảm ơn vì đăng! –

+0

Điều này làm việc cho tôi ... Cảm ơn !!!! : D –

5

Nếu bạn muốn sử dụng mùa xuân Đậu trong lớp CXF Web Service, sau đó tuyên bố WebService như sau trong tập tin cấu hình XML của CXF (ví dụ như lò xo cxf.xml)

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

Declare tách đậu cho Lớp WebService và sau đó đặt nó vào điểm cuối với một ID. Như thế này, bạn sẽ có bean được quản lý vào mùa xuân, nơi bạn cũng có thể sử dụng chú thích AutoWired.

Đậu của bạn sẽ không bao giờ được tự động tiêm nếu bạn khai báo dịch vụ web của mình như sau.

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

Trong trường hợp này, bạn sẽ cần một trong hai:

  • Tiêm đậu mùa xuân bằng tay

    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

  • Hoặc lấy đậu từng người một từ mùa xuân ngữ cảnh

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

    Tôi chưa thử điều này cho JAX-RS, nhưng cách tiếp cận theo ý kiến ​​của tôi cũng giống nhau.

    From CXF official documentation.

Các vấn đề liên quan