在WebService的implementor中用@Autowired注入用@Component,@Resposrity,@Service注解标记的bean,都失败了,得到的都是null。google一番后找到了结果,只要改变一下配置文件写法即可。
package aitajian.ws;
import javax.annotation.Resource;
import javax.jws.WebService;
import aitajian.entity.Order;
import aitajian.mapper.ReimburseMapper;
@WebService
public class OrderWsIpml implements OrderWs {
@Autowired
privat@Autowirede ReimburseMapper rm;
public OrderWsIpml() {
System.out.println("OrderWsIpml()");
}
@Override
public Order getOrderById(int id) {
System.out.println("server getOrderById() "+id);
System.out.println(rm+"-------------");
System.out.println(rm.getReimburse(3));
return new Order(id,"飞机",1000000);
}
}
原配置:
<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">
<!-- 引入cxf的一些核心配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint
id="orderWS"
implementor="aitajian.ws.OrderWsIpml"
address="/orderws" />
</beans>
新配置:
<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">
<!-- 引入cxf的一些核心配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean name="orderWsIpml" class="aitajian.ws.OrderWsIpml"/>
<jaxws:endpoint
id="orderWS"
implementor="#orderWsIpml"
address="/orderws" />
</beans>
主要区别是原本jaxws中的implementor属性是直接写实现类的fullClass,现在改成ref bean的方式。
也可以使用@Component注解标记实现类为一个bean,然后implementor属性用# + bean的名字来reference这个bean即可。这和使用配置文件的方式来配置bean,写法是大同小异的。