File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does autowiring work?

 
Jitesh Sinha
Ranch Hand
Posts: 146
  • 0
  • 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
  • 0
  • 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: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic