Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

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?

Ramakrishna K.C
author & internet detective
Posts: 41086
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.
Posts: 1682
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.
They worship nothing. They say it's because nothing lasts forever. Like this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    Bookmark Topic Watch Topic
  • New Topic