Spring provides a powerful feature called component scanning. It can automatically scan, detect, and instantiate your components with particular stereotype annotations from the classpath. Below are showed the main annotations and the possible ways to register and scan them.


To ask Spring to auto-wire the bean properties with @Autowired or @Resource, you have to register an AutowiredAnnotationBeanPostProcessor instance in the IoC container as following:

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

If you are using a bean factory, you have to register this bean post processor through the API. Otherwise, you can just declare an instance of it in your application context. Or you can simply include the element below in your bean configuration file:


An AutowiredAnnotationBeanPostProcessorinstance will automatically get registered.

@Component (@Repository/@Service/@Controller)

By default, Spring will detect all classes annotated with @Component, @Repository, @Service, @Controller, or your custom annotation type that is itself annotated with @Component. You can customize the scan by applying one or more include/exclude filters.

With the stereotype annotations applied to your component classes, you can ask Spring to scan them by declaring a single XML element.
In this element, you need to specify the package for scanning your components. Then the specified package and all its subpackages will be scanned. You can use commas to separate multiple packages for scanning.
The required element is:

<context:component-scan base-package="package.to.scan" />


You can enable declarative transaction management declaring the element:

<tx:annotation-driven />


You have to register a PersistenceAnnotationBeanPostProcessorinstance to inject entity managers into properties annotated with @PersistenceContext as below:

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

A PersistenceAnnotationBeanPostProcessorinstance will be registered automatically once you enable the element:

<context:annotation-config />