【问题标题】:MethodInvokingFactoryBean returns itself instead of desired objectMethodInvokingFactoryBean 返回自身而不是所需的对象
【发布时间】:2020-02-21 08:35:55
【问题描述】:

我正在尝试使用MethodInvokingFactoryBean 来获取com.amazonaws.regions.Region 的实例以用于配置com.amazonaws.services.kinesis.AmazonKinesisClient。我正在 Blueprint、Camel、Karaf 中执行此操作。

<bean id="awsRegion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="com.amazonaws.regions.RegionUtils"/>
    <property name="targetMethod" value="getRegion"/>
    <property name="arguments">
        <list>
            <value>EU-WEST-1</value>
        </list>
    </property>
</bean>

<bean id="kinesisClient" class="com.amazonaws.services.kinesis.AmazonKinesisClient">
    <property name="region" ref="awsRegion"/>
</bean>

这似乎应该可以工作,第一个 bean 创建一个区域,第二个 bean 使用它。

但是,我收到一个错误,看起来 MethodInvokingFactoryBean 只是返回一个自身的实例,而不是 Region。

org.osgi.service.blueprint.container.ComponentDefinitionException: Error setting property: PropertyDescriptor <name: region, getter: null, setter: [class com.amazonaws.AmazonWebServiceClient.setRegion(class com.amazonaws.regions.Region)]
...
Caused by: java.lang.Exception: Unable to convert value org.springframework.beans.factory.config.MethodInvokingFactoryBean@2289c050 to type com.amazonaws.regions.Region

我在 RegionUtils 中调用的方法应该返回一个 Region https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/regions/RegionUtils.html#getRegion-java.lang.String-

我在这个问题中遇到了这种在客户端中获取区域的方法,该解决方案似乎对提问者有效。

Bind aws sqs region camel

【问题讨论】:

  • 弹簧和蓝图不要混用,只用蓝图

标签: spring apache-camel spring-bean blueprint-osgi


【解决方案1】:

首先,apache-camel 标记在这里是多余的。其次,您错过了询问者正在使用弹簧上下文并且您正在使用蓝图上下文。试试这样的:

<bean id="awsRegion" class="com.amazonaws.regions.RegionUtils" factory-method="getRegion">
    <argument value="EU-WEST-1"/>
</bean>

<bean id="kinesisClient" class="com.amazonaws.services.kinesis.AmazonKinesisClient">
    <property name="region" ref="awsRegion"/>
</bean>

编辑:刚刚用最新的 aws-java-sdk 测试了这个例子,它可以工作

【讨论】:

  • 嗯,这会稍微改变错误,但仍然不起作用:RuntimeException: Unable to convert to class com.amazonaws.regions.Region
  • Edit please question with stacktrace of new exception
  • 嗯,刚刚又试了一次,确实有效。一定是把你的答案复制粘贴错了什么的。虽然 region 参数应该是小写的。
猜你喜欢
  • 1970-01-01
  • 2019-08-26
  • 2021-06-29
  • 2012-09-28
  • 2019-11-18
  • 1970-01-01
  • 2014-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多