转自:
http://www.myexception.cn/software-architecture-design/882646.html
CXF中使用Autowired注解无法注入bean的问题
之前学习了CXF,通过官方文档,学下来没什么大问题。今天用到实际项目中,在WebService的implementor中用@Autowired注入用@Component,@Resposrity,@Service注解标记的bean,都失败了,得到的都是null。google一番后找到了结果,只要改变一下配置文件写法即可。
implementor:
@WebService(endpointInterface = "demo.hw.server.HelloWorld", serviceName = "HelloWorld")
@Component(value="helloWorldImpl")
public class HelloWorldImpl implements HelloWorld {
@Autowired
private TestBean tBean;
Map<Integer, User> users = new LinkedHashMap<Integer, User>();
public String sayHi(String text) {
System.out.println("sayHi called");
System.out.println(tBean);
if (tBean == null) {
return "Hello " + text;
}else{
return "Hello " + tBean.getValue();
}
}
}
原配置:
<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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 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://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <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" /> <context:component-scan base-package="demo" /> <jaxws:endpoint id="helloWorld" implementor="demo.hw.server.HelloWorldImpl" address="/HelloWorld"> </jaxws:endpoint> </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"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
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://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<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" />
<context:component-scan base-package="demo" />
<jaxws:endpoint id="helloWorld" implementor="#helloWorldImpl"
address="/HelloWorld">
</jaxws:endpoint>
</beans>
主要区别是原本jaxws中的implementor属性是直接写实现类的fullClass,现在改成ref bean的方式,使用@Component注解标记实现类为一个bean,然后implementor属性用# + bean的名字来reference这个bean即可。如果你使用的配置文件的方式来配置bean,其写法也是大同小异的。