• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Autowiring or setters?

 
Greenhorn
Posts: 4
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I got an advice from our architect saying to avoid autowiring and use setter injection. What do you think is best and why?
I see autowiring (if done properly, of course) a way to simplify code writing and better navigation within large projects.

Regards,
Gabi
 
Ranch Hand
Posts: 433
Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gabi Teris wrote:
I see autowiring (if done properly, of course) a way to simplify code writing and better navigation within large projects.


Have you ever had to deal with a large project which was not familiar to you and where autowiring was used? Where do you see a better navigation when autowiring is used?
Yes, it saves a lot of typework. But in my experience it can get really ugly if it's not working as expected. Using setters is a bit more work but in my opionion it's the more maintainable approach.
And if you need to mention "if done properly, of course" (on any development issue) you can be sure: someone somewhere is doing right now the exact opposite (because of dead-lines, pressure, lack of sleep and/or knowledge, whatever).
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we were also having a discussion about @Autowire over here.
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I second that. Explicit wiring gives you tool support for navigating that is not possible with autowiring. Sure, you spare yourself a little typing but you don't get this for free. You loose tool support (STS, but there is experimental support, IIRC), readibility (I don't think less code automatically results in better readable code), bug avoiding (the earlier, the cheaper - autowiring moves some bug occurences to the run-time, the latest possible stage).

Could you explain why you think autowiring leads to "better navigation"?
 
Gabi Teris
Greenhorn
Posts: 4
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for sharing your opinion!
Now I'm more than convinced not to use autowiring.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There may be good reasons for using it; in my opinion it is just against the basic reason why to use Java: Find bugs early by supplying as much (type) information at compile time as possible. (It's certainly not about the language being great from an academic point of view.)

There are other approaches like "duck typing" (if it has a "quack" method then I will use it, without knowing about the type or intention). Dynamic languages are gaining poopularity recently. For me autowiring looks a lot like it. It's fine for itself, but combining the two leads to a loss at both ends. (Still have to write verbose Java, but bugs can still occur at late stages.)
reply
    Bookmark Topic Watch Topic
  • New Topic