-
Jan 27th, 2010, 11:59 AM
#1
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...
-
Jan 27th, 2010, 03:56 PM
#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
-
Jan 28th, 2010, 02:52 AM
#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
-
Jan 28th, 2010, 03:01 AM
#4
<bean id="beanA">
<property name="prop1" value="val1"/>
</bean>
<bean id="beanB">
<property name="prop2" value="#{beanA.prop1}"/>
</bean>
-Hetal
-
Jan 28th, 2010, 03:25 AM
#5
@Hetal B
the property of "bean1" a is already valorized, i wan't to set it at "val1"
-
Jan 28th, 2010, 03:34 AM
#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
-
Jan 28th, 2010, 03:43 AM
#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?
-
Jan 28th, 2010, 03:51 AM
#8
-
Jan 28th, 2010, 03:55 AM
#9

Originally Posted by
Hetal B
I'm reading these pages but i don't understand what kind of method i have to invoke....
-
Jan 28th, 2010, 03:56 AM
#10
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
-
Forum Rules