Spring 5 auto scanning using @Component annotation and XML configuration


Spring framework has the functionality to auto-detect or auto scan the bean’s classes for injection using @Component annotation. @Component is a generic stereotype for any Spring-managed component. In the most of the 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 to add the context namespace schema to the root beans tag and scan the package. See the below configuration.

<beans 
    //...
    xmlns:context="http://www.springframework.org/schema/context"
    //...
    xsi:schemaLocation="http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

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

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

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.

State.java
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.

Country.java
package org.websparrow.beans;

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

@Component
public class Country {

	@Autowired
	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.

spring.xml
<?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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 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" />
	</bean>
	
</beans>

How does it work?

There is a question that nocks 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.

Test.java
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);
		c.display();
	}
}
Output:

The following result will display on your console log.

State name is: Uttar Pradesh

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.