Apache Camel和Spring的类型转换问题

时间:2018-02-23 09:11:29

标签: java spring types apache-camel

我正在尝试将我的Camel Spring(Camel 2.20.2; Spring 4.3.14)配置外部化为application.properties属性文件。此属性文件包含以下内容:

net.sender1.port = 47000
net.sender1.address = 127.255.255.255

application.properties文件位于src/main/resources内,并通过maven-shade-plugin复制到目标jar。

我的Camel上下文如下所示:

<?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>

当我启动应用程序时,出现以下错误:

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)

似乎正确解析了address属性,因为它是一个String。这意味着应用程序可以找到属性文件。

有没有办法在属性或spring xml中明确定义类型?

2 个答案:

答案 0 :(得分:4)

您的应用程序实际上找不到您的属性文件。它试图将字符串文字“$ {net.sender1.port}”转换为Integer,这就是你得到NumberFormatException的原因。

您需要指定PropertyPlaceholder才能读取外部属性文件。

<bean class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer" id="bridgePropertyPlaceholder">
    <property name="location" value="classpath:application.properties"/>
</bean>

如果将上述内容添加到您的应用程序中,它至少应该能够读取实际属性。

答案 1 :(得分:0)

您也可以使用跟随构造

<beans ...
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="...
         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
         ">
  <context:property-placeholder properties-ref="properties"/>
  <util:properties id="properties" location="classpath:application.properties"/>

</beans>