This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call setter on one bean with getter from other bean

 
Cory Max
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to initialize one bean property with the property of another bean. For example:

 
Hong Anderson
Ranch Hand
Posts: 1936
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't just use value="blue"?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Armando Flores Ibarra
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you can try using the @PostConstruct annotation in Shirt´s color setter.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic