cxf+spring开发web service

环境: cxf-2.1.3,jdk6,jboss7.0.2,spring3.0.6
用cxf+spring开发web service程序很简单,不过有一些集成问题要注意。在此把这几天发现的一些总结一下,最后有一个遗留的问题
1、关于bean的声明
要发布或者要调用的web service接口,需要用@WebService注解声明。不过要注意的是,@WebService注解不会把类声明为spring的bean
可以声明为bean的方式有以下4个:
<jaxws:endpoint> <jaxws:client> <bean id="" class=""> @Component
写了一个类来证明这一点:

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.                             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.                             http://www.springframework.org/schema/context   
  9.                             http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  10.                             http://cxf.apache.org/jaxws   
  11.                             http://cxf.apache.org/schemas/jaxws.xsd">  
  12.   
  13.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  14.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  15.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  16.       
  17.     <context:component-scan base-package="com.huawei.framework" />  
  18.       
  19.     <bean id="helloWorldImpl" class="com.huawei.framework.webservice.HelloWorldImpl">  
  20.         <property name="remedy" ref="remedy" />  
  21.     </bean>  
  22.   
  23.     <jaxws:endpoint id="helloWorld" address="/HelloWorld"  
  24.         implementor="#helloWorldImpl" />  
  25.   
  26.     <jaxws:client id="remedy"  
  27.         serviceClass="com.huawei.remedy.webservice.RemedyWebServiceCM"  
  28.         address="http://10.78.229.199:8080/remedy/webservice/RemedyWebService" />  
  29.   
  30. </beans>  
<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       						http://www.springframework.org/schema/context 
      			 			http://www.springframework.org/schema/context/spring-context-3.0.xsd
        					http://cxf.apache.org/jaxws 
        					http://cxf.apache.org/schemas/jaxws.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="com.huawei.framework" />
	
	<bean id="helloWorldImpl" class="com.huawei.framework.webservice.HelloWorldImpl">
		<property name="remedy" ref="remedy" />
	</bean>

	<jaxws:endpoint id="helloWorld" address="/HelloWorld"
		implementor="#helloWorldImpl" />

	<jaxws:client id="remedy"
		serviceClass="com.huawei.remedy.webservice.RemedyWebServiceCM"
		address="http://10.78.229.199:8080/remedy/webservice/RemedyWebService" />

</beans>
Java代码 复制代码 收藏代码
  1. protected void doGet(HttpServletRequest request,  
  2.             HttpServletResponse response) throws ServletException, IOException {  
  3.   
  4.         ServletContext context = request.getServletContext();  
  5.   
  6.         WebApplicationContext wc = WebApplicationContextUtils  
  7.                 .getWebApplicationContext(context);  
  8.   
  9.         String[] beans = wc.getBeanDefinitionNames();  
  10.   
  11.         for (String beanName : beans) {  
  12.             System.out.println(beanName);  
  13.         }  
  14.           
  15.         RemedyWebServiceCM remedy = (RemedyWebServiceCM) wc.getBean("remedy");  
  16.           
  17.         AcknowledgeRequest message = new AcknowledgeRequest();  
  18.         remedy.acknowledge(message);  
  19.   
  20.     }  
protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		ServletContext context = request.getServletContext();

		WebApplicationContext wc = WebApplicationContextUtils
				.getWebApplicationContext(context);

		String[] beans = wc.getBeanDefinitionNames();

		for (String beanName : beans) {
			System.out.println(beanName);
		}
		
		RemedyWebServiceCM remedy = (RemedyWebServiceCM) wc.getBean("remedy");
		
		AcknowledgeRequest message = new AcknowledgeRequest();
		remedy.acknowledge(message);

	}

在控制台可以看到以下几个bean: 20:02:24,120 INFO  [stdout] (http--0.0.0.0-8888-2) helloWorldImpl 20:02:24,120 INFO  [stdout] (http--0.0.0.0-8888-2) helloWorld 20:02:24,120 INFO  [stdout] (http--0.0.0.0-8888-2) remedy
以上3个bean,就分别是由<bean>、<jaxws:endpoint>、<jaxws:client>生成的
2、如果要在<jaxws:endpoint>的实现类中注入bean的话,要注意:
错误的写法:

Xml代码 复制代码 收藏代码
  1. <jaxws:endpoint id="helloWorld" address="/HelloWorld"  
  2.         implementor="com.huawei.framework.webservice.HelloWorldImpl" />  
<jaxws:endpoint id="helloWorld" address="/HelloWorld"
		implementor="com.huawei.framework.webservice.HelloWorldImpl" />

用这种写法,虽然HelloWorldImpl类已经被声明成bean了,但是注入是失败的
准确来说,在spring容器中存在有HelloWorldImpl对应的bean,并且相关的依赖也已经注入了。但是cxf框架得到的HelloWorldImpl,与上述的bean不是同一个对象,这个HelloWorldImpl对象可能是用反射或者其他机制创建出来的,没有任何组件被注入
正确的写法:

Xml代码 复制代码 收藏代码
  1. <jaxws:endpoint id="helloWorld" address="/HelloWorld"  
  2.         implementor="#helloWorldImpl" />  
<jaxws:endpoint id="helloWorld" address="/HelloWorld"
		implementor="#helloWorldImpl" />

在implementor属性中,用#beanName,这样的话,cxf框架得到的HelloWorldImpl,就是spring容器持有的那个bean。当然这有个前提,就是系统中已经存在这个bean。用<bean id="">或者@Component都是OK的,这里支持注解
3、上面说的是<jaxws:endpoint>,比较简单。但是<jaxws:client>就比较麻烦
我目前感觉,<jaxws:client>声明的bean,没办法在别的bean里用@Autowired注入,只能用<bean id="">的方式来注入

Java代码 复制代码 收藏代码
  1. @WebService  
  2. public interface RemedyWebServiceCM {  
  3.   
  4.     RemedyResponse acknowledge(AcknowledgeRequest arg0);  
  5.   
  6.     RemedyResponse close(CloseRequest arg0);  
  7.   
  8.     RemedyResponse notify(NotifyRequest arg0);  
  9.   
  10. }  
@WebService
public interface RemedyWebServiceCM {

	RemedyResponse acknowledge(AcknowledgeRequest arg0);

	RemedyResponse close(CloseRequest arg0);

	RemedyResponse notify(NotifyRequest arg0);

}
Java代码 复制代码 收藏代码
  1. public class HelloWorldImpl implements HelloWorld {  
  2.   
  3.     private RemedyWebServiceCM remedy;  
  4.   
  5.     public String sayHi(User theUser) {  
  6.           
  7.         AcknowledgeRequest message = new AcknowledgeRequest();  
  8.         remedy.acknowledge(message);  
  9.           
  10.         return "Hello " + theUser.getName() + " ,your age is "  
  11.                 + theUser.getAge();  
  12.     }  
  13.   
  14.     public void setRemedy(RemedyWebServiceCM remedy) {  
  15.         this.remedy = remedy;  
  16.     }  
  17.       
  18. }  
public class HelloWorldImpl implements HelloWorld {

	private RemedyWebServiceCM remedy;

	public String sayHi(User theUser) {
		
		AcknowledgeRequest message = new AcknowledgeRequest();
		remedy.acknowledge(message);
		
		return "Hello " + theUser.getName() + " ,your age is "
				+ theUser.getAge();
	}

	public void setRemedy(RemedyWebServiceCM remedy) {
		this.remedy = remedy;
	}
	
}
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.                             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.                             http://www.springframework.org/schema/context   
  9.                             http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  10.                             http://cxf.apache.org/jaxws   
  11.                             http://cxf.apache.org/schemas/jaxws.xsd">  
  12.   
  13.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  14.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  15.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  16.       
  17.     <context:component-scan base-package="com.huawei.framework" />  
  18.       
  19.     <bean id="helloWorldImpl" class="com.huawei.framework.webservice.HelloWorldImpl">  
  20.         <property name="remedy" ref="remedy" />  
  21.     </bean>  
  22.   
  23.     <jaxws:endpoint id="helloWorld" address="/HelloWorld"  
  24.         implementor="#helloWorldImpl" />  
  25.   
  26.     <jaxws:client id="remedy"  
  27.         serviceClass="com.huawei.remedy.webservice.RemedyWebServiceCM"  
  28.         address="http://10.78.229.199:8080/remedy/webservice/RemedyWebService" />  
  29.   
  30. </beans>  
<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       						http://www.springframework.org/schema/context 
      			 			http://www.springframework.org/schema/context/spring-context-3.0.xsd
        					http://cxf.apache.org/jaxws 
        					http://cxf.apache.org/schemas/jaxws.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="com.huawei.framework" />
	
	<bean id="helloWorldImpl" class="com.huawei.framework.webservice.HelloWorldImpl">
		<property name="remedy" ref="remedy" />
	</bean>

	<jaxws:endpoint id="helloWorld" address="/HelloWorld"
		implementor="#helloWorldImpl" />

	<jaxws:client id="remedy"
		serviceClass="com.huawei.remedy.webservice.RemedyWebServiceCM"
		address="http://10.78.229.199:8080/remedy/webservice/RemedyWebService" />

</beans>

用这种<bean id="">的方式就可以

Java代码 复制代码 收藏代码
  1. @Controller  
  2. public class HelloWorldImpl implements HelloWorld {  
  3.   
  4.     @Autowired  
  5.     private RemedyWebServiceCM remedy;  
  6.   
  7.     public String sayHi(User theUser) {  
  8.           
  9.         AcknowledgeRequest message = new AcknowledgeRequest();  
  10.         remedy.acknowledge(message);  
  11.           
  12.         return "Hello " + theUser.getName() + " ,your age is "  
  13.                 + theUser.getAge();  
  14.     }  
  15.       
  16. }  
@Controller
public class HelloWorldImpl implements HelloWorld {

	@Autowired
	private RemedyWebServiceCM remedy;

	public String sayHi(User theUser) {
		
		AcknowledgeRequest message = new AcknowledgeRequest();
		remedy.acknowledge(message);
		
		return "Hello " + theUser.getName() + " ,your age is "
				+ theUser.getAge();
	}
	
}
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.                             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.                             http://www.springframework.org/schema/context   
  9.                             http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  10.                             http://cxf.apache.org/jaxws   
  11.                             http://cxf.apache.org/schemas/jaxws.xsd">  
  12.   
  13.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  14.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  15.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  16.       
  17.     <context:component-scan base-package="com.huawei.framework" />  
  18.       
  19.     <jaxws:endpoint id="helloWorld" address="/HelloWorld"  
  20.         implementor="#helloWorldImpl" />  
  21.   
  22.     <jaxws:client id="remedy"  
  23.         serviceClass="com.huawei.remedy.webservice.RemedyWebServiceCM"  
  24.         address="http://10.78.229.199:8080/remedy/webservice/RemedyWebService" />  
  25.   
  26. </beans>  
<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       						http://www.springframework.org/schema/context 
      			 			http://www.springframework.org/schema/context/spring-context-3.0.xsd
        					http://cxf.apache.org/jaxws 
        					http://cxf.apache.org/schemas/jaxws.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="com.huawei.framework" />
	
	<jaxws:endpoint id="helloWorld" address="/HelloWorld"
		implementor="#helloWorldImpl" />

	<jaxws:client id="remedy"
		serviceClass="com.huawei.remedy.webservice.RemedyWebServiceCM"
		address="http://10.78.229.199:8080/remedy/webservice/RemedyWebService" />

</beans>

用@Autowired加@Component的方式来做,就不行,报以下异常:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.huawei.remedy.webservice.RemedyWebServiceCM com.huawei.framework.webservice.HelloWorldImpl.remedy; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.huawei.remedy.webservice.RemedyWebServiceCM] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) [org.springframework.beans-3.0.6.RELEASE.jar:] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) [org.springframework.beans-3.0.6.RELEASE.jar:] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) [org.springframework.beans-3.0.6.RELEASE.jar:] ... 21 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.huawei.remedy.webservice.RemedyWebServiceCM] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924) [org.springframework.beans-3.0.6.RELEASE.jar:] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793) [org.springframework.beans-3.0.6.RELEASE.jar:] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707) [org.springframework.beans-3.0.6.RELEASE.jar:] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) [org.springframework.beans-3.0.6.RELEASE.jar:] ... 23 more
说自动注入失败,因为找不到RemedyWebServiceCM类型的bean
试了很久,完全搞不定。也想不通是为啥,因为RemedyWebServiceCM这个类型的bean,明明已经用<jaxws:client>声明了,而且用配置文件的方式也是能搞定的

环境: 
cxf-2.1.3,jdk6,jboss7.0.2,spring3.0.6 

用cxf+spring开发web service程序很简单,不过有一些集成问题要注意。在此把这几天发现的一些总结一下,最后有一个遗留的问题 

1、关于bean的声明 

要发布或者要调用的web service接口,需要用@WebService注解声明。不过要注意的是,@WebService注解不会把类声明为spring的bean 

可以声明为bean的方式有以下4个: 

<jaxws:endpoint> 
<jaxws:client> 
<bean id="" class=""> 
@Component 

写了一个类来证明这一点: 



Xml代码 复制代码 收藏代码
1.<?xml version="1.0" encoding="UTF-8"?>  
2.  
3.<beans xmlns="http://www.springframework.org/schema/beans"  
4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"  
5.    xmlns:context="http://www.springframework.org/schema/context"  
6.    xsi:schemaLocation="http://www.springframework.org/schema/beans  
7.                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
8.                            http://www.springframework.org/schema/context   
9.                            http://www.springframework.org/schema/context/spring-context-3.0.xsd  
10.                            http://cxf.apache.org/jaxws   
11.                            http://cxf.apache.org/schemas/jaxws.xsd">  
12.  
13.    <import resource="classpath:META-INF/cxf/cxf.xml" />  
14.    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
15.    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
16.      
17.    <context:component-scan base-package="com.huawei.framework" />  
18.      
19.    <bean id="helloWorldImpl" class="com.huawei.framework.webservice.HelloWorldImpl">  
20.        <property name="remedy" ref="remedy" />  
21.    </bean>  
22.  
23.    <jaxws:endpoint id="helloWorld" address="/HelloWorld"  
24.        implementor="#helloWorldImpl" />  
25.  
26.    <jaxws:client id="remedy"  
27.        serviceClass="com.huawei.remedy.webservice.RemedyWebServiceCM"  
28.        address="http://10.78.229.199:8080/remedy/webservice/RemedyWebService" />  
29.  
30.</beans>  





Java代码 复制代码 收藏代码
1.protected void doGet(HttpServletRequest request,  
2.            HttpServletResponse response) throws ServletException, IOException {  
3.  
4.        ServletContext context = request.getServletContext();  
5.  
6.        WebApplicationContext wc = WebApplicationContextUtils  
7.                .getWebApplicationContext(context);  
8.  
9.        String[] beans = wc.getBeanDefinitionNames();  
10.  
11.        for (String beanName : beans) {  
12.            System.out.println(beanName);  
13.        }  
14.          
15.        RemedyWebServiceCM remedy = (RemedyWebServiceCM) wc.getBean("remedy");  
16.          
17.        AcknowledgeRequest message = new AcknowledgeRequest();  
18.        remedy.acknowledge(message);  
19.  
20.    }  


在控制台可以看到以下几个bean: 
20:02:24,120 INFO  [stdout] (http--0.0.0.0-8888-2) helloWorldImpl 
20:02:24,120 INFO  [stdout] (http--0.0.0.0-8888-2) helloWorld 
20:02:24,120 INFO  [stdout] (http--0.0.0.0-8888-2) remedy 

以上3个bean,就分别是由<bean>、<jaxws:endpoint>、<jaxws:client>生成的 

2、如果要在<jaxws:endpoint>的实现类中注入bean的话,要注意: 

错误的写法: 



Xml代码 复制代码 收藏代码
1.<jaxws:endpoint id="helloWorld" address="/HelloWorld"  
2.        implementor="com.huawei.framework.webservice.HelloWorldImpl" />  


用这种写法,虽然HelloWorldImpl类已经被声明成bean了,但是注入是失败的 

准确来说,在spring容器中存在有HelloWorldImpl对应的bean,并且相关的依赖也已经注入了。但是cxf框架得到的HelloWorldImpl,与上述的bean不是同一个对象,这个HelloWorldImpl对象可能是用反射或者其他机制创建出来的,没有任何组件被注入 

正确的写法: 



Xml代码 复制代码 收藏代码
1.<jaxws:endpoint id="helloWorld" address="/HelloWorld"  
2.        implementor="#helloWorldImpl" />  


在implementor属性中,用#beanName,这样的话,cxf框架得到的HelloWorldImpl,就是spring容器持有的那个bean。当然这有个前提,就是系统中已经存在这个bean。用<bean id="">或者@Component都是OK的,这里支持注解 

3、上面说的是<jaxws:endpoint>,比较简单。但是<jaxws:client>就比较麻烦 

我目前感觉,<jaxws:client>声明的bean,没办法在别的bean里用@Autowired注入,只能用<bean id="">的方式来注入 



Java代码 复制代码 收藏代码
1.@WebService  
2.public interface RemedyWebServiceCM {  
3.  
4.    RemedyResponse acknowledge(AcknowledgeRequest arg0);  
5.  
6.    RemedyResponse close(CloseRequest arg0);  
7.  
8.    RemedyResponse notify(NotifyRequest arg0);  
9.  
10.}  





Java代码 复制代码 收藏代码
1.public class HelloWorldImpl implements HelloWorld {  
2.  
3.    private RemedyWebServiceCM remedy;  
4.  
5.    public String sayHi(User theUser) {  
6.          
7.        AcknowledgeRequest message = new AcknowledgeRequest();  
8.        remedy.acknowledge(message);  
9.          
10.        return "Hello " + theUser.getName() + " ,your age is "  
11.                + theUser.getAge();  
12.    }  
13.  
14.    public void setRemedy(RemedyWebServiceCM remedy) {  
15.        this.remedy = remedy;  
16.    }  
17.      
18.}  





Xml代码 复制代码 收藏代码
1.<?xml version="1.0" encoding="UTF-8"?>  
2.  
3.<beans xmlns="http://www.springframework.org/schema/beans"  
4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"  
5.    xmlns:context="http://www.springframework.org/schema/context"  
6.    xsi:schemaLocation="http://www.springframework.org/schema/beans  
7.                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
8.                            http://www.springframework.org/schema/context   
9.                            http://www.springframework.org/schema/context/spring-context-3.0.xsd  
10.                            http://cxf.apache.org/jaxws   
11.                            http://cxf.apache.org/schemas/jaxws.xsd">  
12.  
13.    <import resource="classpath:META-INF/cxf/cxf.xml" />  
14.    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
15.    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
16.      
17.    <context:component-scan base-package="com.huawei.framework" />  
18.      
19.    <bean id="helloWorldImpl" class="com.huawei.framework.webservice.HelloWorldImpl">  
20.        <property name="remedy" ref="remedy" />  
21.    </bean>  
22.  
23.    <jaxws:endpoint id="helloWorld" address="/HelloWorld"  
24.        implementor="#helloWorldImpl" />  
25.  
26.    <jaxws:client id="remedy"  
27.        serviceClass="com.huawei.remedy.webservice.RemedyWebServiceCM"  
28.        address="http://10.78.229.199:8080/remedy/webservice/RemedyWebService" />  
29.  
30.</beans>  


用这种<bean id="">的方式就可以 



Java代码 复制代码 收藏代码
1.@Controller  
2.public class HelloWorldImpl implements HelloWorld {  
3.  
4.    @Autowired  
5.    private RemedyWebServiceCM remedy;  
6.  
7.    public String sayHi(User theUser) {  
8.          
9.        AcknowledgeRequest message = new AcknowledgeRequest();  
10.        remedy.acknowledge(message);  
11.          
12.        return "Hello " + theUser.getName() + " ,your age is "  
13.                + theUser.getAge();  
14.    }  
15.      
16.}  





Xml代码 复制代码 收藏代码
1.<?xml version="1.0" encoding="UTF-8"?>  
2.  
3.<beans xmlns="http://www.springframework.org/schema/beans"  
4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"  
5.    xmlns:context="http://www.springframework.org/schema/context"  
6.    xsi:schemaLocation="http://www.springframework.org/schema/beans  
7.                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
8.                            http://www.springframework.org/schema/context   
9.                            http://www.springframework.org/schema/context/spring-context-3.0.xsd  
10.                            http://cxf.apache.org/jaxws   
11.                            http://cxf.apache.org/schemas/jaxws.xsd">  
12.  
13.    <import resource="classpath:META-INF/cxf/cxf.xml" />  
14.    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
15.    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
16.      
17.    <context:component-scan base-package="com.huawei.framework" />  
18.      
19.    <jaxws:endpoint id="helloWorld" address="/HelloWorld"  
20.        implementor="#helloWorldImpl" />  
21.  
22.    <jaxws:client id="remedy"  
23.        serviceClass="com.huawei.remedy.webservice.RemedyWebServiceCM"  
24.        address="http://10.78.229.199:8080/remedy/webservice/RemedyWebService" />  
25.  
26.</beans>  


用@Autowired加@Component的方式来做,就不行,报以下异常: 

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.huawei.remedy.webservice.RemedyWebServiceCM com.huawei.framework.webservice.HelloWorldImpl.remedy; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.huawei.remedy.webservice.RemedyWebServiceCM] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) [org.springframework.beans-3.0.6.RELEASE.jar:] 
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) [org.springframework.beans-3.0.6.RELEASE.jar:] 
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) [org.springframework.beans-3.0.6.RELEASE.jar:] 
	... 21 more 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.huawei.remedy.webservice.RemedyWebServiceCM] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924) [org.springframework.beans-3.0.6.RELEASE.jar:] 
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793) [org.springframework.beans-3.0.6.RELEASE.jar:] 
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707) [org.springframework.beans-3.0.6.RELEASE.jar:] 
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) [org.springframework.beans-3.0.6.RELEASE.jar:] 
	... 23 more 

说自动注入失败,因为找不到RemedyWebServiceCM类型的bean 

试了很久,完全搞不定。也想不通是为啥,因为RemedyWebServiceCM这个类型的bean,明明已经用<jaxws:client>声明了,而且用配置文件的方式也是能搞定的 

 

posted @ 2017-07-21 08:53  sky20080101  阅读(109)  评论(0)    收藏  举报