Results 1 to 7 of 7

Thread: creating beans that are arrays

  1. #1
    Join Date
    Jul 2008
    Posts
    5

    Unhappy 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">
    <constructor-arg>
    <value>10101010101010101</value>
    </constructor-arg>
    </bean>

  2. #2
    Join Date
    Jan 2008
    Location
    Mohnton, PA USA (that's near Philadelphia)
    Posts
    2,148

    Default

    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: http://static.springframework.org/sp...ans-conversion

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

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

  3. #3
    Join Date
    Jul 2008
    Posts
    5

    Default

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

  4. #4
    Join Date
    Jul 2008
    Posts
    5

    Default 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"/>
    </bean>

  5. #5
    Join Date
    Jan 2008
    Location
    Mohnton, PA USA (that's near Philadelphia)
    Posts
    2,148

    Default

    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. #6
    Join Date
    Jul 2008
    Posts
    5

    Default

    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. #7
    Join Date
    Jul 2008
    Posts
    5

    Default

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •