2013-02-15 36 views
0

我試圖做這樣的事情:如何設置一個只要十六進制在Spring XML配置

<bean id="myBean" class="com.example.SomeThing"> 
    <property name="longValue"> 
     <bean class="java.lang.Long"> 
      <constructor-arg value="0x0418a14372d4186eL" type="long"/> 
     </bean> 
    </property> 
</bean> 

但是這會導致以下異常:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'java.lang.Long#19190b75' defined in file [/apollo/env/path/to/config.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments? 
+0

這工作,以及:'<屬性名= 「的longValue」 值= 「0x0418a14372d4186e」/>' – 2017-01-19 19:15:11

回答

1

我想你想factory-method參數<bean>

<bean id="myBean" class="com.example.SomeThing"> 
    <property name="longValue"> 
     <bean class="java.lang.Long" factory-method="decode"> 
      <constructor-arg value="0x0418a14372d4186" /> 
     </bean> 
    </property> 
</bean> 

(注缺少了 「L」 後綴的。)

0

SPEL應該工作

<bean id="myBean" class="com.example.SomeThing"> 
    <property name="longValue" value="#{0x0418a14372d4186eL}" /> 
</bean> 
+0

這一個心不是爲我工作:java.lang.NumberFormatException:致對於輸入字符串:「{ 0x0418a14372d4136eL}「 – Sam 2013-02-15 20:16:32

+0

你忘了添加#嗎?因爲SPeL清楚地解析十六進制 – 2013-02-15 20:22:02

1

你可以使用Java @Configuration

@Configuration 
public class Config { 

    @Bean 
    public SomeThing someThing() { 
     final SomeThing bean = new SomeThing(); 
     bean.set(Long.parseLong("0418a14372d4186e", 16)); 
     return bean; 
    } 

} 
相關問題