Auto Discovery

Dependency injection can be done by xml configuration or auto-wiring. Auto wiring significantly minimizes size of XML file.

In order to further reduce XML configuration you can define beans with following annotations:

  1. @Component: creates simple beans

  2. @Repository: creates DAOs

  3. @Service: creates Services

  4. @Controller: creates controllers

These annotated beans and their dependencies can be automatically discovered <context:component-scan tag in applicationContext.xml file. With help of auto-discovery you do not need to manually register the beans in spring config file.

Here is sample configuration that auto discovers beans from com.sunilos.book.spring package.

<context:component-scan base-package="com.sunilos.book.spring" />

or

@ComponentScan annotation

FAQ

Q: Which beens are auto discovered?

A: All annotated beans like @Repository, @Service, @Controller etc.

Q: Which annotation do you use to do auto-discovery ?

A: @ComponentScan annotation