[英]Native CXF integration in grails
有人知道如何在不使用cxf插件的情況下集成cxf框架嗎? 我已經發布了一個簡單的服務,但是我的問題是將現有的grails服務bean注入cxf jaxws bean中。
在applicationContext.xml中,我正在使用以下定義
<jaxws:server id="jaxwsService" serviceClass="at.pdts.cxf.HelloWorld" address="/hello">
<jaxws:serviceBean>
<bean class="at.pdts.cxf.HelloWorldImpl">
<property name="halloService"><ref bean="helloWorld"></ref></property>
</bean>
</jaxws:serviceBean>
</jaxws:server>
helloWorld bean是普通的grails服務類。 在啟動過程中,我得到以下異常。
設置bean屬性“ halloService”時無法解析對bean“ helloWorld”的引用; 嵌套的異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有定義名為'helloWorld'的bean
applicationContext.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"
xsi:schemaLocation="
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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-soap.xml"/>
<bean id="grailsApplication" class="org.codehaus.groovy.grails.commons.GrailsApplicationFactoryBean">
<description>Grails application factory bean</description>
<property name="grailsDescriptor" value="/WEB-INF/grails.xml" />
<property name="grailsResourceLoader" ref="grailsResourceLoader" />
</bean>
<bean id="pluginManager" class="org.codehaus.groovy.grails.plugins.GrailsPluginManagerFactoryBean">
<description>A bean that manages Grails plugins</description>
<property name="grailsDescriptor" value="/WEB-INF/grails.xml" />
<property name="application" ref="grailsApplication" />
</bean>
<bean id="grailsConfigurator" class="org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator">
<constructor-arg>
<ref bean="grailsApplication" />
</constructor-arg>
<property name="pluginManager" ref="pluginManager" />
</bean>
<bean id="grailsResourceLoader" class="org.codehaus.groovy.grails.commons.GrailsResourceLoaderFactoryBean">
<property name="grailsResourceHolder" ref="grailsResourceHolder" />
</bean>
<bean id="grailsResourceHolder" scope="prototype" class="org.codehaus.groovy.grails.commons.spring.GrailsResourceHolder">
<property name="resources">
<value>classpath*:**/grails-app/**/*.groovy</value>
</property>
</bean>
<bean id="characterEncodingFilter"
class="org.springframework.web.filter.CharacterEncodingFilter">
<property name="encoding">
<value>utf-8</value>
</property>
</bean>
<jaxws:server id="jaxwsService" serviceClass="at.pdts.cxf.HelloWorld" address="/hello">
<jaxws:serviceBean>
<bean class="at.pdts.cxf.HelloWorldImpl">
<property name="halloService"><ref bean="halloService"></ref></property>
</bean>
</jaxws:serviceBean>
</jaxws:server>
</beans>
HelloWorldImpl.groovy
package at.pdts.cxf
import javax.jws.WebService
@WebService(endpointInterface = "at.pdts.cxf.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
def halloService // inject HelloService. Initialize this bean via applicationContext.xml
public String sayHi(String text) {
return "hello " + halloService.scream(text)
}
}
HelloService.groovy
class HalloService implements InitializingBean{
static transactional = false
String scream(String text) {
text.toUpperCase()
}
// methods gets not called, so service bean is not initialized at the ws creation time
void afterPropertiesSet() {
println "------> initializing bean HalloSerivce <--------
}
}
似乎在jaxwsService初始化時,helloWorld服務bean不可用。
這需要指出:
<ref bean="helloWorld">
您是否有定義如下的內容:
<bean id="helloWorld" class="at.pdts.cxf.HalloServiceImpl" />
該錯誤意味着Spring現在可以找到別名為“ helloWorld”的spring bean。
也許將整個spring.xml和Java代碼發布到HelloWorldImpl會有所幫助。
編輯:您的配置證實了我的理論。
<ref bean=
說“在這里注入其他東西”。 但是您尚未定義該bean,因此沒有No Such Bean Definition。 此外,我可以使用返回一個空字符串的自定義尖叫方法創建自己的HalloService(HalloServiceImpl)實現,從而使您的代碼正常工作。 然后,將其添加到spring配置中: <bean id="helloWorld" class="at.pdts.cxf.HalloServiceImpl" />
編輯#2:使其工作的另一種方法是消除HalloService:
<jaxws:server id="jaxwsService" serviceClass="at.pdts.cxf.HelloWorld" address="/hello">
<jaxws:serviceBean>
<bean class="at.pdts.cxf.HelloWorldImpl" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
HelloWorldImpl.groovy
package at.pdts.cxf
import javax.jws.WebService
@WebService(endpointInterface = "at.pdts.cxf.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
return "hello scream!" + text
}
}
基本上,您的選擇是:為Spring提供HalloService的實現,或者不在Spring.xml中引用它。
編輯#3:關於InitializingBean的目的存在誤解:
從javadoc:
Bean實現的InitializingBean接口,需要在BeanFactory設置完所有屬性后作出反應的bean:例如,執行自定義初始化,或僅檢查是否已設置所有必需屬性。
實現InitializingBean只是意味着將調用afterPropertiesSet()。 這並不意味着春天將這個bean自動添加到您的Spring配置。 您仍然必須使用以下行在spring配置中聲明bean:
<bean id="halloService" class="at.pdts.cxf.HalloService" />
我第一次讀到您的問題時就想念這個,但是您正在applicationContext.xml
中定義bean。 在為您的問題做一個測試用例時,我將我的bean定義放在grails-app/conf/spring/resources.xml
。 嘗試創建該文件並將以下內容放入其中:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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" />
<!--create the bean for the service, link to groovy service bean -->
<jaxws:server id="jaxwsService" serviceClass="at.pdts.cxf.HelloWorld" address="/hello">
<jaxws:serviceBean>
<bean class="at.pdts.cxf.HelloWorldImpl">
<property name="halloService" ref="halloService" />
</bean>
</jaxws:serviceBean>
</jaxws:server>
</beans>
作為附帶說明,您可以在此處找到有關集成Grails和CXF的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.