Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does autowiring work?

 
Jitesh Sinha
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does Spring container scans all objects as given in config file via package pattern(e.g. com.mycomp.myproj.*) and finds out which class is implementing which interface ?How is it done so efficiently?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does scan all classes that match the given pattern - but it looks for annotations instead of interfaces. Details are covered in this section of the Spring docs.

It's also not necessarily "efficient" - it just loads the classes and uses reflection to find matching annotations. To prevent long startup times, it's better to be very specific in the pattern that you tell it to match - if you pass it a package path that contains a bunch of classes, it will take time to scan all of them.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathan Pruett wrote:It does scan all classes that match the given pattern - but it looks for annotations instead of interfaces. Details are covered in this section of the Spring docs.

It's also not necessarily "efficient" - it just loads the classes and uses reflection to find matching annotations. To prevent long startup times, it's better to be very specific in the pattern that you tell it to match - if you pass it a package path that contains a bunch of classes, it will take time to scan all of them.


Actually, you just describe component-scan not autowiring. ;)

For autowiring, Spring looks at just the Spring Beans that it created for an object of the type that needs to be injected into the other bean. You don't/can't filter this. Spring will go through all your beans that it knows about, it won't go outside of those classes.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic