- 浏览: 1821820 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (665)
- 闲话 (17)
- ruby (1)
- javascript (40)
- linux (7)
- android (22)
- 开发过程 (11)
- 哥也读读源代码 (13)
- JVM (1)
- ant (2)
- Hibernate (3)
- jboss (3)
- web service (17)
- https (4)
- java基础 (17)
- spring (7)
- servlet (3)
- 杂记 (39)
- struts2 (10)
- logback (4)
- 多线程 (2)
- 系统诊断 (9)
- UI (4)
- json (2)
- Java EE (7)
- eclipse相关 (4)
- JMS (1)
- maven (19)
- 版本管理 (7)
- sso (1)
- ci (1)
- 设计 (18)
- 戒烟 (4)
- http (9)
- 计划 (4)
- HTML5 (3)
- chrome extensions (5)
- tomcat源码阅读 (4)
- httpd (5)
- MongoDB (3)
- node (2)
最新评论
-
levin_china:
勾选了,还是找不到
用spring annotation声明的bean,当打包在jar中时,无法被扫描到 -
GGGGeek:
我用的maven-3.5.0,还没有遇到这种情况,使用jar ...
用spring annotation声明的bean,当打包在jar中时,无法被扫描到 -
GGGGeek:
受益匪浅,从组织项目结构,到技术细节,讲的很到位,只是博主不再 ...
一个多maven项目聚合的实例 -
Aaron-Joe-William:
<?xml version="1.0" ...
hibernate逆向工程 -
li272355201:
http://archive.apache.org/dist/ ...
tomcat源码阅读(一)——环境搭建
环境:
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
写了一个类来证明这一点:
在控制台可以看到以下几个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的话,要注意:
错误的写法:
用这种写法,虽然HelloWorldImpl类已经被声明成bean了,但是注入是失败的
准确来说,在spring容器中存在有HelloWorldImpl对应的bean,并且相关的依赖也已经注入了。但是cxf框架得到的HelloWorldImpl,与上述的bean不是同一个对象,这个HelloWorldImpl对象可能是用反射或者其他机制创建出来的,没有任何组件被注入
正确的写法:
在implementor属性中,用#beanName,这样的话,cxf框架得到的HelloWorldImpl,就是spring容器持有的那个bean。当然这有个前提,就是系统中已经存在这个bean。用<bean id="">或者@Component都是OK的,这里支持注解
3、上面说的是<jaxws:endpoint>,比较简单。但是<jaxws:client>就比较麻烦
我目前感觉,<jaxws:client>声明的bean,没办法在别的bean里用@Autowired注入,只能用<bean id="">的方式来注入
用这种<bean id="">的方式就可以
用@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>声明了,而且用配置文件的方式也是能搞定的
如果是跑controller里,是注入成功了.
刚是在junit跑测试报错,但是依然很奇怪,为什么在junit注入失败~
持续解决中~
果然...测试类中要加上junit的注入配置啊
web.xml我是将applicationContext-api.xml和applicationContext.xml一起配置了.
但是junit没走web.xml.所以才注入失败....
如果是跑controller里,是注入成功了.
刚是在junit跑测试报错,但是依然很奇怪,为什么在junit注入失败~
持续解决中~
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 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>
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的话,要注意:
错误的写法:
<jaxws:endpoint id="helloWorld" address="/HelloWorld" implementor="com.huawei.framework.webservice.HelloWorldImpl" />
用这种写法,虽然HelloWorldImpl类已经被声明成bean了,但是注入是失败的
准确来说,在spring容器中存在有HelloWorldImpl对应的bean,并且相关的依赖也已经注入了。但是cxf框架得到的HelloWorldImpl,与上述的bean不是同一个对象,这个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="">的方式来注入
@WebService public interface RemedyWebServiceCM { RemedyResponse acknowledge(AcknowledgeRequest arg0); RemedyResponse close(CloseRequest arg0); RemedyResponse notify(NotifyRequest arg0); }
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 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="">的方式就可以
@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 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>声明了,而且用配置文件的方式也是能搞定的
评论
7 楼
hegengxi
2015-08-26
错误的写法:
Xml代码
<jaxws:endpoint id="helloWorld" address="/HelloWorld"
implementor="com.huawei.framework.webservice.HelloWorldImpl" />
=======================================================================
赞,解决了好几天都没解决的问题,太棒了
Xml代码
<jaxws:endpoint id="helloWorld" address="/HelloWorld"
implementor="com.huawei.framework.webservice.HelloWorldImpl" />
=======================================================================
赞,解决了好几天都没解决的问题,太棒了
6 楼
liukai
2014-03-19
liukai 写道
liukai 写道
我也碰到这个问题了.
无法注入成功.楼主最后解决了么?
无法注入成功.楼主最后解决了么?
如果是跑controller里,是注入成功了.
刚是在junit跑测试报错,但是依然很奇怪,为什么在junit注入失败~
持续解决中~
果然...测试类中要加上junit的注入配置啊
@ContextConfiguration({ "classpath:applicationContext.xml" , "classpath:applicationContext-api.xml"})
web.xml我是将applicationContext-api.xml和applicationContext.xml一起配置了.
但是junit没走web.xml.所以才注入失败....
5 楼
liukai
2014-03-19
liukai 写道
我也碰到这个问题了.
无法注入成功.楼主最后解决了么?
无法注入成功.楼主最后解决了么?
如果是跑controller里,是注入成功了.
刚是在junit跑测试报错,但是依然很奇怪,为什么在junit注入失败~
持续解决中~
4 楼
liukai
2014-03-19
我也碰到这个问题了.
无法注入成功.楼主最后解决了么?
无法注入成功.楼主最后解决了么?
3 楼
kyfxbl
2012-10-25
是的,不用激动,呵呵
2 楼
ssetnegl1990
2012-10-25
哥们华为公司的???看包得命名!!
1 楼
zhaoshunxin
2012-04-21
错误的写法:
Xml代码
<jaxws:endpoint id="helloWorld" address="/HelloWorld"
implementor="com.huawei.framework.webservice.HelloWorldImpl" />
cxf2.5.2已经支持了这种写法了
Xml代码
<jaxws:endpoint id="helloWorld" address="/HelloWorld"
implementor="com.huawei.framework.webservice.HelloWorldImpl" />
cxf2.5.2已经支持了这种写法了
发表评论
-
XML-RPC与web service
2013-06-05 12:44 2096前段时间公司的办公环 ... -
cxf集成spring,精简版
2012-08-15 17:40 2916上次总结了spring集成cxf的方法,不过有些地方说得不清楚 ... -
替换证书,造成bad_certificate
2012-05-14 21:47 5285系统调用外部的web service,走https方式,今天测 ... -
cxf配置http:conduit
2012-05-08 20:16 10780本次项目要通过https方式,调用外部的web service ... -
cxf生成endpoint,使用soap1.1和soap1.2
2012-04-25 23:31 3071开发了一个web service服务,客户居然不认,查看客户发 ... -
cxf配置方式实现日志和https请求
2012-04-21 01:21 7809这篇博客介绍一下怎么通过配置方式配置https和cxf的日志功 ... -
一次艰难的web service对接开发
2012-04-19 23:10 3689这次的需求是将我们的系统与一个客户系统对接,对接的方式是通过w ... -
根据已有的wsdl,开发web service的服务端和客户端
2012-04-11 23:13 18137折腾了好长时间,今天终于把这次web service对接的需求 ... -
用cxf生成的方式,开发web service应用
2012-04-09 23:08 9800这次开发web service对接,用的是cxf2.0.13 ... -
解决cxf的jar包冲突
2012-03-28 20:23 15882本次做web service开发,遇到了一些问题,最终解决了, ... -
用soapUI调试web service
2012-03-27 21:00 2988用soapUI调试web service是很方便的,可以省掉自 ... -
对web service和cxf的个人理解
2012-02-29 23:59 1813个人理解很粗浅,只是谈谈感受,希望能抛砖引玉。就从本次项目的实 ... -
用cxf发布和调用web service
2012-02-29 23:03 17987最近我们的系统需要和一个第三方系统对接,对接的方式是通过web ... -
cxf和spring mvc的集成
2012-02-29 22:27 17749Spring MVC是通过DispatcherServlet来 ... -
我个人理解的什么是web service
2012-01-05 20:26 1788有一些乱七八糟的想法,还不成章法,先记录下来,以后再回头看看 ... -
关于B/S和C/S的想法,兼谈web service
2011-08-24 20:31 3139最近做的这个项目,是 ...
相关推荐
Apache cxf 与spring集成
CXF集成Spring发布WebService代码实例
CXF跟Spring集成的 WebServer实例 由于大小限制 没有上传Jar包,Cxf版本为 apache-cxf-2.3.0 可以官网下载http://cxf.apache.org/
NULL 博文链接:https://ynp.iteye.com/blog/888547
CXF 集成 mybatis spring 环境搭建,以及开发所需要的CXF JAR 包
cxf与spring集成 需要最少的jar如下: cxf-2.3.3.jar geronimo-annotation_1.0_spec-1.1.1.jar geronimo-jaxws_2.2_spec-1.0.jar geronimo-stax-api_1.0_spec-1.0.1.jar geronimo-ws-metadata_2.0_spec-1.1.3.jar ...
cxf集成Spring的restful WebServ ice接口,Demo的完整代码!
webService实现 cxf2.7.5 与 spring3.0集成 完整流程,内涵全部jar ,下载集合运行
cxf-2.2.9.jar geronimo-javamail_1.4_spec-1.7.1.jar geronimo-jaxws_2.2_spec-1.1.jar geronimo-jms_1.1_spec-1.1.1.jar geronimo-servlet_3.0_spec-1.0.jar jaxb-api-2.2.6.jar jaxb-impl-2.2.6.jar neethi-3.0.1...
自己在用的接口框架,实现了CXF和spring和hibernate的集成
Cxf集成Spring的WebService接口的配置文档
用webservice中的xfire框架升级到cxf框架开发的实例,供大家分享~~~~
cxf+spring+hibernate集成整合jar包
Spring是目前最流行的JavaEE Framework,但是使用Spring的Spring-WS开发WebService却...CXF是一个简化WebService开发的开源项目,通过Spring和CXF的结合可以大大简化基于Spring Framework的应用中的WebService开发。
CXF2.5.0+Spring2.5 集成DEMO
使用最新的CXF2.7和Spring3的 Java WebService 集成用例 应用Spring Annotation
自己总结的一个CXF集成Spring的例子,可以直接运行,附源码! 程序的详细说明请参照博客文档 http://blog.csdn.net/sunbaojin5213344/article/details/8931600
绝对可运行,里面有客户端和服务端还有对应的操作文档,导入eclipse或者MyEclipse十分钟内就搞定,虽然贵点但是绝对物有所值.
详细介绍Cxf集成与Spring 实现WebService.
Spring集成Cxf调用WebServices