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
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. –