Announcement Announcement Module
Collapse
No announcement yet.
How to "map" a source bean to a destination bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


    • #3
      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

      Comment


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

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

        -Hetal

        Comment


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

          Comment


          • #6
            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

            Comment


            • #7
              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?

              Comment


              • #8
                This may help:-

                http://static.springsource.org/sprin...ssions-beandef

                -Hetal

                Comment


                • #9
                  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....

                  Comment


                  • #10
                    You are configuring your bean definitions using xml right ?

                    Comment


                    • #11
                      <bean id="mybean" class="net.roseindia.Inject" >
                      <property name="name" value="#{sb.name}"/>
                      </bean>

                      and the output is

                      Name: #{sb.name}



                      Comment


                      • #12
                        finally i solved using dozer

                        but if someone has solution using spring directly, i'm here

                        Thanks

                        Comment


                        • #13
                          Hope you did include the spring expression jar.

                          Comment


                          • #14
                            Originally posted by Hetal B View Post
                            Hope you did include the spring expression jar.
                            ops :o

                            Comment

                            Working...
                            X