我想在我的彈簧集成配置中使用特定場景的Groovy腳本。有幾個錯誤我無法解決,或者超出了我的理解範圍。請原諒,因爲我不熟悉groovy腳本。Spring集成Groovy腳本和SpEL
我在我的庫中有spring-integration-groovy-3.0.2.RELEASE.jar
。
下面是我的春天一體化配置:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration" xmlns:context="http://www.springframework.org/schema/context"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
xmlns:int-groovy="http://www.springframework.org/schema/integration/groovy" xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Works fine: Using service-activator and SpEL to call a java bean.method(params) -->
<int:chain input-channel="requestChannel" output-channel="responseChannel">
<int-xml:xslt-transformer xsl-templates="BookingClassFilter_Template" result-transformer="resultToDOMSource" />
<int:service-activator expression="@errorDetectionUtil.checkAndThrowExceptionOnError(#root)" />
</int:chain>
// Note: I want to replace the above int:chain with the below. Only the below section appears in my final configuration file deployed. The above int:chain is only as an example for Q&A.
<!-- NOT WORKING: Using Groovy script -->
<int:chain id="businessLayerResponse" input-channel="requestChannel" output-channel="responseChannel">
<int-xml:xslt-transformer xsl-templates="BookingClassFilter_Template" result-transformer="resultToDOMSource" />
<int-groovy:script>
def valBool = errorDetectionUtil.hasErrors(payload)
if(valBool) throw new AppException(#root)
return #root
</int-groovy:script>
</int:chain>
</beans>
下面是我的Java類:
import javax.xml.transform.dom.DOMSource;
@Component("errorDetectionUtil")
public class ErrorDetectionUtil {
public boolean hasErrors(DOMSource domSource) {
double countDbl = (double) errorsCounter.evaluate(domSource.getNode(), XPathConstants.NUMBER);
return ((int) countDbl > 0)
}
public Message<?> checkAndThrowExceptionOnError(Message<?> message) throws AppException {
if (hasErrors((DOMSource) message.getPayload())) {
throw new AppException(message);
}
return message;
}
}
// AppException extends org.springframework.integration.MessagingException
我不知道我在做什麼錯在這裏。下面是我啓動應用程序服務器時看到的異常stacktrace。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.integration.handler.MessageHandlerChain#13': Cannot create inner bean 'businessLayerResponse$child#2.handler' of type [org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor] while setting bean property 'handlers' with key [2]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'businessLayerResponse$child#2.handler': Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'componentName' of bean class [org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor]: Bean property 'componentName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:282)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:121)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:353)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:154)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1387)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1128)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4729)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:586)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1750)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'businessLayerResponse$child#2.handler': Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'componentName' of bean class [org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor]: Bean property 'componentName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1423)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1128)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:271)
... 30 more
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'componentName' of bean class [org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor]: Bean property 'componentName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1042)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:902)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:57)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1420)
... 34 more
謝謝Artem!我使用''內的''做了實現更改。是的,我擺脫了'componentName'異常。但是我需要在'AppException'中發送'Message >'對象。有沒有一種方法可以在拋出'AppException'時設置'Message'對象?例如'拋出新的AppException(#root)' –
Nashvi
很高興聽到!如果這對你很好,接受答案是好的基調。 –
我剛剛編輯了我以前的評論。你能再請參考嗎?謝謝! – Nashvi