Apache Camel 和 Spring 的类型转换问题 [英] Type conversion issue with Apache Camel and Spring
问题描述
我正在尝试将我的 Camel Spring(Camel 2.20.2;Spring 4.3.14)配置具体化为一个 application.properties
属性文件.该属性文件具有以下内容:
net.sender1.port = 47000net.sender1.address = 127.255.255.255
application.properties
文件位于 src/main/resources
中,并由 maven-shade-plugin 复制到目标 jar.
我的 Camel 上下文如下所示:
<bean id="udpSender1" class="com.foo.MyUDPSender"><constructor-arg type="java.lang.String" value="${net.sender1.address}"/><constructor-arg type="java.lang.Integer" value="${net.sender1.port}"/></bean><camelContext xmlns="http://camel.apache.org/schema/spring">...</camelContext></豆类>
当我启动应用程序时,出现以下错误:
<前>警告 |2018-02-23 09:50:25,324 |[main] ClassPathXmlApplicationContext.refresh - 上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为udpSender1"的 bean 在类路径资源 [META-INF/spring/camel-context 中定义时出错.xml]:通过构造函数参数 1 表示的不满足的依赖关系:无法将类型 [java.lang.String] 的参数值转换为所需类型 [java.lang.Integer]:无法转换类型java.lang.String"的值需要类型'java.lang.Integer';嵌套异常是 java.lang.NumberFormatException:对于输入字符串:${net.sender1.port}"org.springframework.beans.factory.UnsatisfiedDependencyException:在类路径资源 [META-INF/spring/camel-context.xml] 中定义名称为udpSender1"的 bean 创建时出错:通过构造函数参数 1 表示的不满足的依赖关系:无法转换参数值类型 [java.lang.String] 到所需类型 [java.lang.Integer]:无法将类型java.lang.String"的值转换为所需类型java.lang.Integer";嵌套异常是 java.lang.NumberFormatException:对于输入字符串:${net.sender1.port}"在 org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723)在 org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)在 org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)在 org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93)在 org.apache.camel.spring.Main.createDefaultApplicationContext(Main.java:222)在 org.apache.camel.spring.Main.doStart(Main.java:154)在 org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)在 com.foo.Server.main(Server.java:19)看来 address
属性被正确解析,因为它是一个字符串.这意味着应用程序可以找到属性文件.
有没有办法在属性或spring xml中显式定义类型?
实际上,您的应用程序找不到您的属性文件.它试图将字符串文字${net.sender1.port}"转换为整数,这就是为什么您会收到 NumberFormatException.
您需要指定一个 PropertyPlaceholder
以读取您的外部属性文件.
<property name="location" value="classpath:application.properties"/></bean>
如果您将上述内容添加到您的应用程序中,它至少应该能够读取实际属性.
I'm trying to externalize my Camel Spring (Camel 2.20.2; Spring 4.3.14) configuration into an application.properties
property file. This property file has the following content:
net.sender1.port = 47000
net.sender1.address = 127.255.255.255
The application.properties
file resides within src/main/resources
and is copied to the target jar by the maven-shade-plugin.
My Camel context looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="..." xmlns:xsi="..." xmlns:camel="..." xsi:schemaLocation="...">
<bean id="udpSender1" class="com.foo.MyUDPSender">
<constructor-arg type="java.lang.String" value="${net.sender1.address}" />
<constructor-arg type="java.lang.Integer" value="${net.sender1.port}" />
</bean>
<camelContext xmlns="http://camel.apache.org/schema/spring">
...
</camelContext>
</beans>
When I start the application, I get the following error:
WARN | 2018-02-23 09:50:25,324 | [main] ClassPathXmlApplicationContext.refresh - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'udpSender1' defined in class path resource [META-INF/spring/camel-context.xml]: Unsatisfied dependency expressed through constructor parameter 1: Could not convert argument value of type [java.lang.String] to required type [java.lang.Integer]: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "${net.sender1.port}" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'udpSender1' defined in class path resource [META-INF/spring/camel-context.xml]: Unsatisfied dependency expressed through constructor parameter 1: Could not convert argument value of type [java.lang.String] to required type [java.lang.Integer]: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "${net.sender1.port}" at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93) at org.apache.camel.spring.Main.createDefaultApplicationContext(Main.java:222) at org.apache.camel.spring.Main.doStart(Main.java:154) at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) at com.foo.Server.main(Server.java:19)
It seems that the address
property is parsed correctly, because it's a String. Which means that the application can find the properties-file.
Is there any way to explicitly define a type in the properties or in the spring xml?
Your properties-file is, in fact, not found by your application. It is trying to convert the String literal "${net.sender1.port}" to an Integer, which is why you get a NumberFormatException.
You need to specify a PropertyPlaceholder
in order to read your external properties-file.
<bean class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer" id="bridgePropertyPlaceholder">
<property name="location" value="classpath:application.properties"/>
</bean>
If you add the above to your application it should at least be able to read the actual properties.
这篇关于Apache Camel 和 Spring 的类型转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!