Spring XML Auto Component Scanning / Mapping Componetns
This example shows how to use spring xml auto component scanning. We will compare the traditional way using only xml to configure your spring beans and use the auto component scanning option. The last will enable spring to automatically detect spring beans annotated with one of the component types this allows spring to automatically register those beans to be auto wired into spring beans. Where spring will handle the injection and lifecycle events.
Manual Component Mapping with Spring Beans
Lets start with the traditional way. Prior to spring 2.5 you needed to configure your beans using an xml file. The only positive I can say about this is that you are able to register a component that is outside of your source code. Beyond this one positive feature I do not recommend to register your project beans anymore by using manual xml components. Use the auto component scanning explained at the bottom.
A simple Java Class
package com.memorynotfound;
public class StepInventory {
public String retrieveStep() {
return "retrieving manual steps...";
}
}
A Simple Service that uses the StepInventory
package com.memorynotfound;
public class StepService {
private StepInventory stepInventory;
public String retrieveStep(){
return stepInventory.retrieveStep();
}
public void setStepInventory(StepInventory stepInventory) {
this.stepInventory = stepInventory;
}
}
Spring xml Configuration
First we register a bean using the
<bean>
element. Inside this element you can specify aclass
attribute which must points to the full qualified name of your class. This enables spring to manage the lifecycle of this class. Which means it can be injected/auto wired into another spring bean.Then we create another spring bean which uses setter injection to wire the other spring bean into the component.
<?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="stepInventory" class="com.memorynotfound.StepInventory"/>
<bean id="stepService" class="com.memorynotfound.StepService">
<property name="stepInventory" ref="stepInventory"/>
</bean>
</beans>
Testing spring xml component mapping
This method will bootstrap spring using the app-config.xml
file located on the class path. Then it’ll lookup the StepService
and execute the method to see if the component is successfully mapped.
package com.memorynotfound;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String...args){
ApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");
StepService stepService = context.getBean(StepService.class);
System.out.println(stepService.retrieveStep());
}
}
Output
Apr 24, 2015 5:53:48 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@44548719: startup date [Fri Apr 24 17:53:48 CEST 2015]; root of context hierarchy
Apr 24, 2015 5:53:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [app-config.xml]
retrieving manual steps...
Spring Auto Component Mapping
In the previous section we saw how to manually register a bean by configuring this in the xml file. Now we’ll take a look at how to auto discover beans using spring xml auto component scanning. Spring can automatically detect stereotyped classes and register corresponding BeanDefinitions with the application context.
StereoTyped Spring Component Annotations
Spring provides a couple of stereotype annotations:
- @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 build a repository
This is annotated by the @Repository
annotation.
package com.memorynotfound;
import org.springframework.stereotype.Repository;
@Repository
public class DancerDao {
public String doDance() {
return "Dancing...";
}
}
Lets build a service
This is annotated by the @Service
annotation.
package com.memorynotfound;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DancerService {
@Autowired
private DancerDao dancerDao;
public String doDance(){
return dancerDao.doDance();
}
}
Spring xml auto component scanning
Lets enable the spring xml auto component scanning by registering a <context:component-scan>
element and configure it with the base-package
attribute. This tells spring which package to scan to discover beans annotated with one of the previous stereotypes.
<context:component-scan>
implicitly enables the functionality of <context:annotation-config>
.<?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.memorynotfound" />
</beans>
Testing spring xml component scanning
package com.memorynotfound;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String...args){
ApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");
DancerService dancerService = context.getBean(DancerService.class);
System.out.println(dancerService.doDance());
}
}
Output
Apr 24, 2015 5:54:16 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5cebc2a2: startup date [Fri Apr 24 17:54:16 CEST 2015]; root of context hierarchy
Apr 24, 2015 5:54:16 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [app-config.xml]
Dancing...