Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: How to "map" a source bean to a destination bean

  1. #1
    Join Date
    Jan 2010
    Posts
    8

    Default How to "map" a source bean to a destination bean

    Hi!
    First of all excuse me if i'd opened this 3d in correct section...

    I would like to inject a property in a bean from an another's bean property...


    Can someone explain me the way to do that?
    Example
    I have MyBean class
    public String getName(){
    return name;
    }

    I would to inject the name property to
    MyBean2 class
    public void setXyz(String xyz){
    this.xyz = xyz;
    }
    I cannot find what i have to write in xml file to do a

    myInstanceOfMyBean2.setXyz(myInstanceOfMyBean.getN ame());

    <bean id="mybean" class="MyBean2" >
    <property name="xyz">
    <B>?????????????????????????????</B>
    </property>
    </bean>

    Can someone help me?



    Excuse me for my english too...

  2. #2
    Join Date
    Jan 2010
    Posts
    9

    Default Straight-forward solution in Spring 3, but a little more tricky in Spring 2

    What version of Spring are you using?

    If you are using Spring 3, this can be accomplished by using the Spring Expression Language (SpEL). You can find information on SpEL in Chapter 6 of the Spring 3 reference documentation.

    If you are using Spring 2, there are a couple options I can think of, but none as direct as the straight-forward option in Spring 3. Let me know if you are using Spring 2, and I will elaborate.

    - SLKF

  3. #3
    Join Date
    Jan 2010
    Posts
    8

    Default

    First of all thanks for helping me!

    I'm using spring 3 and now i'll read Chapter 6 of the Spring 3 reference documentation

    very Thanks

  4. #4
    Join Date
    Jun 2009
    Posts
    190

    Default

    <bean id="beanA">
    <property name="prop1" value="val1"/>
    </bean>

    <bean id="beanB">
    <property name="prop2" value="#{beanA.prop1}"/>
    </bean>

    -Hetal

  5. #5
    Join Date
    Jan 2010
    Posts
    8

    Default

    @Hetal B
    the property of "bean1" a is already valorized, i wan't to set it at "val1"

  6. #6
    Join Date
    Jun 2009
    Posts
    190

    Default

    If i am not wrong this will set the prop2 of beanB with the same value as the value of the property of beanA prop1 i.e. it will set beanB's prop2 to val1.

    -Hetal

  7. #7
    Join Date
    Jan 2010
    Posts
    8

    Default

    Take a look here:

    SourceBean sb = new SourceBean();
    sb.setName("myName");
    XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("myxml.xml"));
    Inject demo = (Inject) beanFactory.getBean("mybean");
    System.out.println(demo);


    What myxml.xml have to contains to valorize mybean.Xyz with value of sb.name?

  8. #8
    Join Date
    Jun 2009
    Posts
    190

  9. #9
    Join Date
    Jan 2010
    Posts
    8

    Default

    Quote Originally Posted by Hetal B View Post
    I'm reading these pages but i don't understand what kind of method i have to invoke....

  10. #10
    Join Date
    Jun 2009
    Posts
    190

    Default

    You are configuring your bean definitions using xml right ?

Posting Permissions

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