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:
1. Using only xml to configure spring beans
We have two java classes like below:
A simple Java Class
1 2 3 4 5 6 7 8 9 10 11 |
package com.javabycode; import org.springframework.stereotype.Repository; public class UserDAO { public String getCurrentUser() { String username = "Administrator"; return username; } } |
A Simple Service that uses the UserDAO class above
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.javabycode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; public class UserService { private UserDAO userDAO; public String getCurrentUser(){ return userDAO.getCurrentUser(); } public void setUserDAO (UserDAO userDAO) { this.userDAO= userDAO; } } |
About Spring xml Configuration
Now we register two beans above with Spring container using the
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <bean id="userDAO" class="com.javabycode.UserDAO"/> <bean id="userService" class="com.javabycode.UserService"> </bean> </beans> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <bean id="userDAO" class="com.javabycode.UserDAO"/> <bean id="userService" class="com.javabycode.UserService"> <property name="userDAO" ref="userDAO"/> </bean> </beans> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.javabycode; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ScanningExample { public static void main(String...args){ ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); UserService userService = context.getBean(UserService.class); System.out.println(userService.getCurrentUser()); } } |
Run the main above and output is printed like below
1 2 3 4 5 |
Feb 15, 2017 6:16:27 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d: startup date [Wed Feb 15 18:16:27 ICT 2017]; root of context hierarchy Feb 15, 2017 6:16:27 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [spring-config.xml] Administrator |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.javabycode; import org.springframework.stereotype.Repository; @Repository public class UserDAO { public String getCurrentUser() { String username = "Administrator"; return username; } } |
Lets create a service
We reuse the UserService class above and annotate this class by the @Service annotation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.javabycode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private UserDAO userDAO; public String getCurrentUser(){ return userDAO.getCurrentUser(); } public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } } |
Spring xml auto component scanning
Instead of using spring xml configuration in the previous section we are registering a
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="com.javabycode" /> </beans> |
Testing Spring Component Scan Example
We can reuse the main above
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.javabycode; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ScanningExample { public static void main(String...args){ ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); UserService userService = context.getBean(UserService.class); System.out.println(userService.getCurrentUser()); } } |
Run the main above and see the output
1 2 3 4 5 |
Feb 16, 2017 9:36:21 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d: startup date [Thu Feb 16 09:36:21 ICT 2017]; root of context hierarchy Feb 16, 2017 9:36:22 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [spring-config.xml] Administrator |
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 (297 downloads)