Spring:将字节数组注入到ArrayList中
将字节数组注入数组列表的正确方法是什么?
private ArrayList<byte[]> bytes;
<property name="bytes">
<list>
<value>0x03,0x4d</value>
</list>
</property>
不起作用,因为每个字符 (0,x,0,3) 都会转换为单个字节。 我也尝试过像  这样的编码,但这也不起作用。
一种解决方法是先转换为整数,然后在 bean 中手动转换回字节,但这不太好。
有什么想法吗?
问候
what is the correct way of injecting a byte array into an arraylist?
private ArrayList<byte[]> bytes;
<property name="bytes">
<list>
<value>0x03,0x4d</value>
</list>
</property>
does not work, as every character (0,x,0,3) is converted into a single byte.
I also tried encoding like , but that doesn't work either.
One workaround would be converting to integer first an then manually convert back to byte in the bean, but that is not too pretty.
Any idea?
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个内置的 ByteArrayPropertyEditor ,但根据 Javadoc,它只将字符串转换为相应的字节数组:
也许你应该 编写自己的属性编辑器将
byte[]
的任意表示形式转换为列表?There is a built-in
ByteArrayPropertyEditor
, but according to the Javadoc, it only converts Strings into corresponding byte array:Maybe you should write your own property editor to convert arbitrary representation of
byte[]
into list?这是一个用于创建字节数组的 FactoryBean:
用法:
(注册一个
PropertyEditor
会更正确,但也会做更多工作)但是如果我正确理解你的评论,你的实际问题是你不知道如何用异国情调的字节编写字符串吗?您可以使用 Unicode 转义:
输出:
当然,您也可以在 Spring XML 文件中使用此语法。
Here's a FactoryBean to create your byte array:
Usage:
(Registering a
PropertyEditor
would be more correct, but also more work)But if I understand your comments right, your actual problem is that you don't know how to write a String with exotic bytes? You can use Unicode escapes:
Output:
You can of course also use this syntax in Spring XML files.