Spring 5 auto scanning using @Component annotation and XML configuration

Spring framework has facility to auto detect or auto scan the beans classes for injection using @Component annotation. @Component is a generic stereotype for any Spring-managed component. In the most of previous example, we use the XML to specify the configuration metadata that produces each BeanDefinition within the Spring container manually.

For auto scanning of beans, we need add the context namespace schema to the root beans tag and scan the package. See the below configuration.


    <!-- scan the package -->
	<context:component-scan base-package="org.websparrow.beans" />

	<!-- activate annotations -->
	<context:annotation-config />

Whenever Spring container will load the configuration file, it will create the object of annotated beans.

Note: It is application only for secondary type dependency injection. For primitive type we have to inject it manually.

In this example, I have used the @Component annotation for auto scanning and @Autowired annotation for automatic dependency injection. Let’s see the complete example.

Spring Beans

Create the two bean classes. In the State class declare a primitive type variable.

package org.websparrow.beans;

public class State {

	// Generate setters and getters...
	private String stateName;

And in Country class, create an object of State class which is a secondary type. Use the @Component annotation at the top of the class name.

package org.websparrow.beans;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

public class Country {

	private State state;

	public void display() {
		System.out.println("State name is: " + state.getStateName());

Spring Beans Configuration

In the configuration add context schema to the root tag bean and scan the package using <context:component-scan base-package="org.websparrow.beans" /> and activate the annotations.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

	<!-- scan the package -->
	<context:component-scan base-package="org.websparrow.beans" />

	<!-- activate annotations -->
	<context:annotation-config />

	<!-- manual injection for primitive type -->
	<bean id="s1" class="org.websparrow.beans.State">
		<property name="stateName" value="Uttar Pradesh" />

How it works?

There is question that nock your mind that how is works or what is the flow of auto scanning. So whenever Spring container will the read the XML configuration, it will scan all the package that you defined in <context:component-scan base-package="org.websparrow.beans" /> and automatically creates the objects of all beans where you annotated by @Component.

Run it

To test it, load the configuration file use J2EE container and run it. But here we don’t have any reference bean id name, pass the class name Country.class as reference bean id.

package org.websparrow.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.websparrow.beans.Country;

public class Test {

	public static void main(String[] args) {

		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		Country c = (Country) context.getBean(Country.class);

The following result will display on your console log.

State name is: Uttar Pradesh

Share this article on: