About Component Scanning
Component scanning called Automatic Configuration
@Component
This annotation is used to identify any class as a component class and Spring understand that a bean should be created for the class.
There is no need to explicitly create bean for the class annotated with this annotation.
Component scanning is not ON by default.
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class AppConfig { }
Here this class is annotated with the @ComponentScan to enable component scanning in spring. By default it will scan in the same package for the component(pojos/beans) annotated with @Component
You can also define the packages for the components.
With @ComponentScan("com.app.data.conponent") or @ComponentScan(basePackages={"com.app.data.component"})
annotation you can define the base packages where to scan for the required components.
It will scan in the defined packages for required component(beans/pojos) which are annotated with @Component
classes annotated witht the @Configuration annotation are eligible for perfoming component scanning in packages.
and they can take advantage of @Autowired and @Inject
One can also enable component scanning by XML using the spring's context namespace.
<context:component-scan base-package="com.raj" />