我正在编写一个 RESTful 服务(在 JBoss 上使用 CXF),其中我使用 Spring(Autowired)注入了另一个类。但是该类没有被注入并且为空。
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) {
.....
}
}
豆类.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 字段中)。它始终为空,我的服务无法按预期工作,而是抛出 NullPointerException
尝试了通过谷歌收集的所有输入。没运气!非常感谢您的帮助。
问候