我正在寫一個RESTful服務(在JBoss上使用CXF),其中我使用Spring(Autowired)注入了另一個類。但是這個類沒有被注入並且是空的。Spring bean未注入CXF Web服務,爲什麼?
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
<?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>
我的rvice是活動的(http://localhost:8080/{warname}/myws/doSomething),但MyCore實例沒有被注入MyWebService(在myCore字段中)。它始終爲空,我的服務無法按預期工作,而是拋出NullPointerException
嘗試通過Google收集的所有輸入。沒有運氣!非常感謝您的幫助。
問候
我嘗試這樣做,並沒有爲我工作。看起來,Web服務不是由Spring創建的,因此不是自動裝配的。 –