Integer.decode() in Spring application context

How to pass an integer value from properties to application, if this value can be stored in octal / decimal / hexadecimal form?

properties example:

 app.size=0120

// should be 80 (dec).

spring context:

<bean id="resource">
    <property name="size">
        <bean class="java.lang.Integer" factory-method="decode">
            <constructor-arg name="nm">
                <bean class="java.lang.String">
                    <constructor-arg value="${app.size}"/>
                </bean>
            </constructor-arg>
        </bean>
    </property>
</bean>

The above doesn't work:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resource' defined in ServletContext resource [/WEB-INF/context.xml]:

Cannot create inner bean 'java.lang.Integer#159c5db' of type [java.lang.Integer] while setting bean property 'size';

nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'java.lang.Integer#159c5db' defined in ServletContext resource [/WEB-INF/context.xml]:

Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: Ambiguous factory method argument types - did you specify the correct bean references as factory method arguments?

I know I can pass size as string, and then decode() it in java code. But it seems right to me to do this transformation in context. Is it possible?


The following works fine with decimal and hexadecimal numbers:

<bean id="resource">
    <property name="size" value="${app.size}" />
</bean>

Thanks to the @Evgeniy Dorofeev's answer.

This worked:

<bean id="resource">
    <property name="size">
        <bean class="java.lang.Integer" factory-method="decode">
            <constructor-arg value="${app.size}" type="java.lang.String" />
        </bean>
    </property>
</bean>
链接地址: http://www.djcxy.com/p/65348.html

上一篇: Spring Security:如何获得applicationContext.xml中的授权用户

下一篇: 在Spring应用程序上下文中的Integer.decode()