Spring Component Scan Example

Spring Component Scan Example shows you how to use spring xml auto component scanning. First, you will be introduced the traditional way using only xml to configure spring beans. Then you will be familiar with the auto component scanning option. The auto component scanning will enable Spring to automatically look up spring beans annotated with one of the Spring Component Annotations. Base on that Spring can automatically register those beans and handle the injection and lifecycle events.

Let’s begin:

Spring Component Scan Example

1. Using only xml to configure spring beans

We have two java classes like below:

A simple Java Class

A Simple Service that uses the UserDAO class above

About Spring xml Configuration

Now we register two beans above with Spring container using the element. This configuration is stored in the spring-config.xml like below:

Noticed that you must specify a class attribute with the full qualified name of your class. This enables spring to manage the lifecycle of this class.

As you know, UserService uses the UserDAO so that you can uses setter injection to wire the UserDAO bean into the UserService. The configuration is updated like below

Testing Spring xml Configuration
We will create main class to test spring xml component mapping configuration above. This class will bootstrap spring container using the spring-config.xml file. Then it will lookup the bean UserService from Spring container.

Run the main above and output is printed like below

2. Spring Component Scan Example
The previous section walks you through manually registering a bean in the xml file. Noticed that Spring can automatically detect classes and register corresponding beans with the application context. Now we show you how to auto look up beans using spring xml auto component scanning.

Spring Component Annotations
Spring provides four types of Spring Stereotype Components:

@Component: is a generic stereotype for any spring-managed component.
@Controller: is typically used for the presentation layer.
@Repository: is typically used for the repository layer
@Service: is typically used for the service layer

Lets create a repository
We reuse the UserDAO class above and annotate this class by the @Repository annotation.

Lets create a service
We reuse the UserService class above and annotate this class by the @Service annotation.

Spring xml auto component scanning
Instead of using spring xml configuration in the previous section we are registering a element and configure it with the base-package attribute in spring xml file. This element tells spring container which package to scan to look up beans annotated with one of four types of Spring Stereotype Components above.

Testing Spring Component Scan Example
We can reuse the main above

Run the main above and see the output

That’s all on the tutorial Spring Component Scan Example. So far, we know two way to configure spring beans: only spring xml configuration and Spring auto component scan. And which one you are prefer to than?

Download complete source code, click link below

spring-component-scan-example.zip (36 downloads)

Leave a Comment

*

Please share it if you found this useful
Hide Buttons