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

  • WendyLG
    started a topic creating beans that are arrays

    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>

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

    Leave a comment:


  • WendyLG
    replied
    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[].

    Leave a comment:


  • oleg.zhurakousky
    replied
    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.

    Leave a comment:


  • WendyLG
    replied
    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>

    Leave a comment:


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

    Leave a comment:


  • oleg.zhurakousky
    replied
    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());

    Leave a comment:

Working...
X