wood burning stoves 2.0*
The moose likes Spring and the fly likes Call setter on one bean with getter from other bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Call setter on one bean with getter from other bean" Watch "Call setter on one bean with getter from other bean" New topic
Author

Call setter on one bean with getter from other bean

Cory Max
Ranch Hand

Joined: Jul 20, 2005
Posts: 83
I would like to initialize one bean property with the property of another bean. For example:



There are only 10 types of people in this world... Those who understand binary and those who don't.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Why don't just use value="blue"?


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Because then the value would have to change in two locations.

Just create a string bean with the value and use it both places.
Cory Max
Ranch Hand

Joined: Jul 20, 2005
Posts: 83
Thanks for both ideas.

I am well aware that I could use the string 'blue' in both places.

The example i gave is a very trivial one for explanation purposes only and my real life problem has more than two objects, they are not cars and shirts either :-).

I am currently making a separate string bean and using that for both objects as the second poster (David) said, but was wondering if my is possible (or not) to do what I asked, call the setter of one bean, passing the getter of another bean as a parameter.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Cory Max wrote:but was wondering if my is possible (or not) to do what I asked, call the setter of one bean, passing the getter of another bean as a parameter.

It's not possible by default, but if you really want it, you could try to implement a custom BeanFactoryPostProcessor.
http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/beans/factory/config/BeanFactoryPostProcessor.html

Anyway, for String value, you could use PropertyPlaceholderConfigurer. It is clearer and cleaner than using String bean.
http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

I'll also add that 99.9% of your beans that you declare in Spring do not hold state, if they do, like a domain object, then they should not be a Spring Bean.

So that handles the cases where the String value is very dynamic and changes often.

That then leads to the case where the String only changes based on the environment it is deployed to. In that case, the PropertyPlaceHolderConfigurer which Kengkaj brought up is the way to go.




Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Armando Flores Ibarra
Greenhorn

Joined: Oct 17, 2009
Posts: 17
Maybe you can try using the @PostConstruct annotation in Shirt´s color setter.


SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Call setter on one bean with getter from other bean
 
Similar Threads
org.springframework.batch.core.repository.JobExecutionAlreadyRunningException
[Spring] Exception while creating the session factory
Bean instantiation from JSP
[Solved][Spring] Unable to load from property files
jsp:getProperty calling bean from specific scope