3

我在属性文件中有一个十六进制值,我希望 Spring 通过在我的 java 对象中使用 @Value 注释将其转换为字节数组。

例子:

hex.value=CB53CD20B2F222D9

在java中我有以下内容:

@Value("#{myProperties['hex.value'] ?: ''}")
private byte[] hexValue;

Spring 目前只是将 String.getBytes() 设置为 hexValue,但我希望它转换为表示字符串的实际字节数组。例如通过使用Commons-Codec Hex

有谁知道如何配置 Spring 使其不只是返回 getBytes()?

4

1 回答 1

1

您可以尝试使用Spring EL 的类型引用

如果您想使用org.apache.commons.codec.binary.Hex#decodeHex,那么目标@Value将如下所示:

@Value("#{T(org.apache.commons.codec.binary.Hex).decodeHex((myProperties['hex.value'] == null ? '' : myProperties['hex.value'] ).toCharArray())}")
private byte[] hexValue;
于 2013-03-19T12:53:49.017 回答