• 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

Difference between @Autowired set in variable and setter-Method of the variable.

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

This is my code,



The above code can be write like this also, observe the @Autowired Annotation place.



Both initializes my Circle correctly. But I dint get the difference between These two codes, In @Autowired positions.
Can anybody explain me with step by step explanation?

Thanks:
Ramakrishna K.C
 
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
If you weren't using autowiring, there is a big difference between constructor and setter injection. You write the XML differently in order to inject the dependencies. And setter injection dependencies are optional while constructor injection dependencies are not.

With autowiring, the only reason I can think of is to avoid a circular dependency problem. If A has B has an autowired dependency to the constructor and B has the same for A, we can't instantiate either of them. Giving one a setter dependency could help with that. (Just speculating, I haven't tried this scenario). I'm curious to see if someone has a better reason.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first way is the more modern approach and that used today in new projects. The second way is the older traditional way, the result is the same. Both are done via reflection. As a matter of fact if your java security policy allows (usually does) the setter is not even required to be there for the @Autowire to work. As Jeanne pointed out sometimes people will use it on constructors to enforce non-optional dependencies, although @Autowired fields are required unless explicitly marked as optional, so once again the result is largely the same. You can also annotate non-constructor/setter methods with @Autowired.

Spring is pretty good about handling cyclic dependencies to a degree but will throw an error if it can't resolve it. These error spell out the problem in a pretty detailed manner.
 
A berm makes a great wind break. And we all like to break wind once in a while. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic