• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Call setter on one bean with getter from other bean

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to initialize one bean property with the property of another bean. For example:

 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't just use value="blue"?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you can try using the @PostConstruct annotation in Shirt´s color setter.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic