1

I am trying initialize a boolean[] in my spring (v3) xml. The catch is that I need to load specific indexes of that array. For example, i want to load bArray below:

  boolean[10] bArray;

  bArray[Options.AUTO]=true;
  bArray[Options.AIR]=false;
  bArray[Options.FOOT]=false;

I've seen example like the one below, but this example does not define the index of the value.

<bean id="MyBean" class="org.test.Autos">
    <property name="lists">
        <util:list list-class="java.util.ArrayList">
            <value>true</value>
            <value>false</value>
            <value>false</value>
        </util:list>
    </property>
</bean>

Can this be done in Spring xml? Thanks

2
  • Have you tried Java @Configuration and doing it... in Java, but still inside Spring container? Commented Aug 1, 2012 at 17:07
  • Not sure I understand what you mean, but I do not have access to the code of the calling class (org.test.Autos in my example). I may end up writing a class to wrap the one and just create instance variables that map to each of the positions in the array. is that what you mean? Commented Aug 1, 2012 at 17:52

1 Answer 1

1

This is an example of what Tomasz means by using the @Configuration annotation :

First you need to define the AutosConfiguration class :

@Configuration
public class AutosConfiguration {

    @Bean
    public boolean[] b_Array() {
        boolean[] bArray = new boolean[10];
        bArray[Options.AUTO]=true;
        bArray[Options.AIR]=false;
        bArray[Options.FOOT]=false;
        return bArray;
    }
}

You will use this class to specify a custom configuration for the bArray variable.

Then you need to configure your spring context :

<context:annotation-config />
<context:component-scan base-package="com.my.pack" />

<bean id="MyBean" class="org.test.Autos">
    <property name="bArray" ref="b_Array" />
</bean>

where com.my.pack is the package where the AutosConfiguration class is found.

I hope this was helpful.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I don't think I was clear. My challenge is that I NEED to define the values in XML (so they can be changed without a code change). So, something like: <util:array array-class="java.lang.String"> <entry index="2">true</entry> <entry index="4">false</entry> <entry index="7">false</entry> </util:array>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.