将int传递给spring属性

时间:2013-02-12 13:05:56

标签: java spring

我想传递 int 而不是字符串,这是spring PropertyPlaceholderConfigurer 中的默认值。

<property name="mailServerPort" value="${mail.port}" />

这给了我错误,因为mailServerPort是int类型而$ {mail.port}是字符串类型。

如何将$ {mail.port}转换为int?

4 个答案:

答案 0 :(得分:10)

根据我的说法,如果port的值是正确的整数,则转换应该正常工作。

如果您遇到问题,可以尝试使用SpringExpressionLanguage(SpEL)进行转换。

<property name="mailServerPort" value="#{ T(java.lang.Integer).parseInt(mail.port) }"/>

希望这会对你有所帮助。

答案 1 :(得分:7)

如果可能,Spring会根据目标类型处理转换。

您确定要设置

吗?
mail.port

使用int?

的值

答案 2 :(得分:0)

Spring应该自动处理它,但是如果它给出了错误,你可以使用 type 属性属性标记并提供并指定require类型。 对于前。

<property name="mailServerPort" type="int" value="${mail.port}" />

希望这会有所帮助......: - )

答案 3 :(得分:0)

如果提供了属性值,以上所有答案都可以正常工作。如果它为空,您可以使用SpringExpressionLanguage来处理该情况并提供默认值:

<property name="mailServerPort" value="#{ '${mail.port}'.isEmpty() ? '8080' : '${mail.port}' }"/>
相关问题