• 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

how does spring handle simple class association

 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


For class B, we don't really need to inject any property to it. it is just a pure utility helper class. Doesn't the above "bean" definition for B look awkward ? What should I do in this case ?
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's fine to just create a bean and set no properties on it.

If you don't need to use the bean anywhere else than in A you can create it inline -

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bear with me, still new to Spring. In this case, in class A, so I do not need to instantiate B b = new B(), right ? I can just use "b" directly since I have defined it in the <bean> section, right ? Is this the only benfit in this case ?

second question --- when the 2nd time class A is invoked, is that "b" the same instance when Class A is invoked ? what's the life cycle of this "b" ?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ben oliver wrote: In this case, in class A, so I do not need to instantiate B b = new B(), right ? I can just use "b" directly since I have defined it in the <bean> section, right ? Is this the only benfit in this case ?


Yes, you can use "b" directly because it is in the bean.xml. This is true regardless of whether you do so using your approach or Nathan's. Nathan's is a bit clearer to me since it shows nobody else is using "b".

ben oliver wrote:second question --- when the 2nd time class A is invoked, is that "b" the same instance when Class A is invoked ? what's the life cycle of this "b" ?


Yes. By default the scope is "singleton" on a Spring bean which means you get the same instance throughout the application.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks.
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic