Announcement Announcement Module
No announcement yet.
creating beans that are arrays Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • creating beans that are arrays

    I am trying to create a bean from my xml configuration file that will end up in hibernates internal method:

    * binary: A type that maps an SQL VARBINARY to a Java byte[].
    * author Gavin King
    protected byte[] toInternalFormat(Object bytes) {
    return (byte[]) bytes;

    I have absolutly no idea how to create a bean that is an array.

    this is the data i am trying to create:
    <bean class="java.lang.byte" id="insertValueTypeLongVarBinary">

  • #2
    There is a concept of PropertyEditors which convert string values to a specific custom type. You can implement your own, but for common types PropertyEditors already exist and you have to do nothing, other then know about their existence and how they operate.
    More info on PropertyEditors is here:

    But, so I don't sound like RTFM guy :-) here is an example:

    Spring configuration (config.xml)
    . . . . . . .
    <bean id="foo" class="org.sample.Foo">
    	<property name="myByteArray" ref="myByteArray"></property>
    <bean id="myByteArray" class="java.lang.String">
    	<constructor-arg value="010"/>
    . . . . . . .
    public class Foo {
    	private byte[] myByteArray;
    	public byte[] getMyByteArray() {
    		return myByteArray;
    	public void setMyByteArray(byte[] myByteArray) {
    		this.myByteArray = myByteArray;
    Test code:
    ApplicationContext ac = new ClassPathXmlApplicationContext("config.xml");
    Foo foo = (Foo) ac.getBean("foo");
    System.out.println("Foo.myByteArray: " + foo.getMyByteArray());


    • #3
      thank you, sorry i just had no idea where to look in the manual, so both the example and the reference are useful


      • #4
        hmm, back at work after the hols and...

        i have created my bean of type byte array property editor, but the only method i can see is "getAsText", what does this offer me over just using a string, as the cast to byte[] still fails. I have read all the documentation i can and it just says this is like saying any other type

        <bean class="org.springframework.beans.propertyeditors.B yteArrayPropertyEditor" id="insertValueTypeLongVarBinary">
        <property name="asText" value="insertValueTypeLongVarBinary"/>


        • #5
          Are you trying to learn PropertyEditors or you trying to create byte array. If you trying to create a byte array then you don't have to do anything special, since property editors for this type are already part if the Spring and the code I provided earlier will work.


          • #6
            ahh, so it does work exactly like string it is started by having a variable of type byte[].

            I have a variable of type Object and I am trying to create a byte[] bean to put into it, so later when it gets to the correct code it can cast it to a byte[].


            • #7
              DOH, as i have got further along object cant store byte[] anyway can it, now i feel stupid