Announcement Announcement Module
Collapse
No announcement yet.
Change value within List problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Change value within List problem

    Hi there,

    I have a problem. I have to change property entries within a list.

    I start with how it looks at the moment:

    Code:
    <bean name="beanA" class="classA" abstract="true">
      <property name="list">
        <list>
          <property name="A" value="1"/>
          <property name="B" value="2"/>
        </list>
      </property>
    </bean>
    
    <bean name="beanB" parent="beanA">
      **** now here I want to change the value of property with name B to 3*****
    </bean>
    How can I do that?

    Just type:
    <property name="list.1.B" value="3"/>
    as B is the second entry in the list?

    Please help me.

    Cheers

  • #2
    If you do following it will just override the list and you'll end up with a list that only has one item,

    Code:
    <bean name="beanB" parent="beanA">
      <property name="list">
        <list>
          <property name="B" value="3"/>
        </list>
      </property>
    </bean>
    Only thing i see you can do is, inside the setter method of the "list" iterate though the existing list and replace the one you want

    Comment


    • #3
      Hey, following code won't work

      Code:
        <property name="list">
          <list>
            <property name="A" value="1"/>
            <property name="B" value="2"/>
          </list>
        </property>
      I don't think you can inject values to a list in that way. It has to be a Map or a Properties to inject values like that (with a key and a value)

      Or are you trying to do something like this?

      Code:
              <list>
                  <bean class="DomainObject">
                      <property name="field" value="2" />
                  </bean>
              </list>

      Comment


      • #4
        So what you can do to override the list (i made it a set, but list also works) is ,

        applilcationContext2.xml

        Code:
        	<bean name="beanA" class="com.client.ClassA" abstract="true">
        		<property name="domains">
        			<list>
        				<bean class="com.client.Domain">
        					<property name="uniqueID" value="1" />
        					<property name="name" value="US" />
        				</bean>
        				<bean class="com.client.Domain">
        					<property name="uniqueID" value="2" />
        					<property name="name" value="UK" />
        				</bean>
        			</list>
        		</property>
        	</bean>
        
        	<bean name="beanB" parent="beanA">
        		<property name="ODomains">
        			<list>
        				<bean class="com.client.Domain">
        					<property name="uniqueID" value="1" />
        					<property name="name" value="CANADA" />
        				</bean>
        			</list>
        		</property>
        	</bean>
        note that in above configurations, domains and ODomains calls two different setters

        Domain.java, overrides the hashcode and equals method to make sure that object equality is based on the value of the uniqueID

        Code:
        package com.client;
        
        public class Domain {
        
        	private int uniqueID;
        
        	private String name;
        
        	public void setName(String name) {
        		this.name = name;
        	}
        
        	public String getName() {
        		return name;
        	}
        
        	@Override
        	public int hashCode() {
        		return uniqueID;
        	}
        
        	@Override
        	public boolean equals(Object object) {
        		if (!(object instanceof Domain)) {
        			return false;
        		}
        		Domain otherDomain = (Domain) object;
        
        		if ((this.uniqueID == 0 && otherDomain.uniqueID != 0)
        				|| (this.uniqueID != 0 && !(this.uniqueID == otherDomain.uniqueID))) {
        			return false;
        		}
        		return true;
        	}
        
        	public void setUniqueID(int uniqueID) {
        		this.uniqueID = uniqueID;
        	}
        
        	public int getUniqueID() {
        		return uniqueID;
        	}
        }
        class A, the class that holds the set. i have added a extra setter method called setODomains. you can replace the code that is marked in red with your object overriding code

        Code:
        package com.client;
        
        import java.util.Set;
        
        public class ClassA {
        
        	private Set<Domain> domains;
        
        	public void setDomains(Set<Domain> domains) {
        		this.domains = domains;
        	}
        
        	public void setODomains(Set<Domain> o) {
        		for (Domain domain : o) {
        			domains.remove(domain);
        			domains.add(domain);
        		}
        	}
        
        	public Set<Domain> getDomains() {
        		return domains;
        	}
        }
        test class,

        Code:
        package com.client;
        
        import org.springframework.context.ConfigurableApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        
        public class MainClass {
        
        	private static ConfigurableApplicationContext appCtx;
        
        	@SuppressWarnings("unused")
        	public static void main(String[] args) {
        		appCtx = new ClassPathXmlApplicationContext(
        				new String[] { "applicationContext2.xml" });
        
        		ClassA classA = (ClassA) appCtx.getBean("beanB");
        
        		for (Domain string : classA.getDomains()) {
        			System.out.println(string.getName());
        		}
        	}
        }
        other thing is, if you want the collection to be in order, you can use a list and do Collections.sort(domainsList, new DomainListComparator())

        Comment


        • #5
          Do this:
          Code:
          <bean name="beanA" class="classA" abstract="true">
            <property name="map">
              <map merge="true">
                <entry key="A" value="1"/>
                <entry key="B" value="2"/>
              </map>
            </property>
          </bean>
          
          <bean name="beanB" class="classB" parent="beanA">
          <property name="map">
              <map merge="true">
                <entry key="B" value="2"/>
              </map>
          </property>
          </bean>

          Comment


          • #6
            Originally posted by pratikbhavsar View Post
            Do this:
            Code:
            <bean name="beanA" class="classA" abstract="true">
              <property name="map">
                <map merge="true">
                  <entry key="A" value="1"/>
                  <entry key="B" value="2"/>
                </map>
              </property>
            </bean>
            
            <bean name="beanB" class="classB" parent="beanA">
            <property name="map">
                <map merge="true">
                  <entry key="B" value="2"/>
                </map>
            </property>
            </bean>
            Discard mine. pratikbhavsar's one is better i misunderstood your problem. thought you wanted to inject some Objects (i.e. Domain) to your list. seems like what you wanted was a map (or properties).
            Last edited by amiladomingo; Jan 13th, 2011, 02:56 PM.

            Comment


            • #7
              Originally posted by pratikbhavsar View Post
              Do this:
              Code:
              <bean name="beanA" class="classA" abstract="true">
                <property name="map">
                  <map merge="true">
                    <entry key="A" value="1"/>
                    <entry key="B" value="2"/>
                  </map>
                </property>
              </bean>
              
              <bean name="beanB" class="classB" parent="beanA">
              <property name="map">
                  <map merge="true">
                    <entry key="B" value="3"/>
                  </map>
              </property>
              </bean>
              just to make sure, after I have done this I will have A=1 and B=3 in my list?

              Comment


              • #8
                just to make sure, after I have done this I will have A=1 and B=3 in my list?
                you mean map right?

                Comment


                • #9
                  yes I meant map It was early in the morning when I answered.

                  Comment


                  • #10
                    Originally posted by Berick View Post
                    yes I meant map It was early in the morning when I answered.
                    Yes it works. Tested it. Thanks to pratikbhavsar.

                    Comment

                    Working...
                    X