Spring Auto scanning components

Normally you declare all the beans or components in XML bean configuration file, so that Spring container can detect and register your beans or components. Actually, Spring is able to auto scan, detect and instantiate your beans from pre-defined project package, no more tedious beans declaration in in XML file.

Following is a simple Spring project, including a customer service and dao layer. Let’s explore the different between declare components manually and auto components scanning in Spring.

1. Declares Components Manually

See a normal way to declare a bean in Spring.

Normal bean.


package com.mkyong.customer.dao;

public class CustomerDAO 
{
	@Override
	public String toString() {
		return "Hello , This is CustomerDAO";
	}	
}

DAO layer.


package com.mkyong.customer.services;

import com.mkyong.customer.dao.CustomerDAO;

public class CustomerService 
{
	CustomerDAO customerDAO;

	public void setCustomerDAO(CustomerDAO customerDAO) {
		this.customerDAO = customerDAO;
	}

	@Override
	public String toString() {
		return "CustomerService [customerDAO=" + customerDAO + "]";
	}
		
}

Bean configuration file (Spring-Customer.xml), a normal bean configuration in Spring.


<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-2.5.xsd">
	
	<bean id="customerService" class="com.mkyong.customer.services.CustomerService">
		<property name="customerDAO" ref="customerDAO" />
	</bean>

	<bean id="customerDAO" class="com.mkyong.customer.dao.CustomerDAO" />

</beans>

Run it


package com.mkyong.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mkyong.customer.services.CustomerService;

public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext context = 
    	  new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});

    	CustomerService cust = (CustomerService)context.getBean("customerService");
    	System.out.println(cust);
    	
    }
}

output


CustomerService [customerDAO=Hello , This is CustomerDAO]

2. Auto Components Scanning

Now, enable Spring auto component scanning features.

Annotate with @Component to indicate this is class is an auto scan component.


package com.mkyong.customer.dao;

import org.springframework.stereotype.Component;

@Component
public class CustomerDAO 
{
	@Override
	public String toString() {
		return "Hello , This is CustomerDAO";
	}	
}

DAO layer, add @Component to indicate this is an auto scan component also.


package com.mkyong.customer.services;

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

import com.mkyong.customer.dao.CustomerDAO;

@Component
public class CustomerService 
{
	@Autowired
	CustomerDAO customerDAO;

	@Override
	public String toString() {
		return "CustomerService [customerDAO=" + customerDAO + "]";
	}
}

Put this “context:component” in bean configuration file, it means, enable auto scanning feature in Spring. The base-package is indicate where are your components stored, Spring will scan this folder and find out the bean (annotated with @Component) and register it in Spring container.


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

	<context:component-scan base-package="com.mkyong.customer" />

</beans>

Run it


package com.mkyong.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mkyong.customer.services.CustomerService;

public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext context = 
    	   new ClassPathXmlApplicationContext(new String[] {"Spring-AutoScan.xml"});

    	CustomerService cust = (CustomerService)context.getBean("customerService");
    	System.out.println(cust);
    	
    }
}

output


CustomerService [customerDAO=Hello , This is CustomerDAO]

This is how auto components scanning works in Spring.

Custom auto scan component name

By default, Spring will lower case the first character of the component – from ‘CustomerService’ to ‘customerService’. And you can retrieve this component with name ‘customerService’.


	CustomerService cust = (CustomerService)context.getBean("customerService");

To create a custom name for component, you can put custom name like this :


@Service("AAA")
public class CustomerService 
...

Now, you can retrieve it with this name ‘AAA’.


	CustomerService cust = (CustomerService)context.getBean("AAA");

Auto Components Scan Annotation Types

In Spring 2.5, there are 4 types of auto components scan annotation types

  • @Component – Indicates a auto scan component.
  • @Repository – Indicates DAO component in the persistence layer.
  • @Service – Indicates a Service component in the business layer.
  • @Controller – Indicates a controller component in the presentation layer.

So, which one to use? It’s really doesn’t matter. Let see the source code of @Repository,@Service or @Controller.


@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {

	String value() default "";

}

You will noticed that all @Repository,@Service or @Controller are annotated with @Component. So, can we use just @Component for all the components for auto scanning? Yes, you can, and Spring will auto scan all your components with @Component annotated.

It’s working fine, but not a good practice, for readability, you should always declare @Repository,@Service or @Controller for a specified layer to make your code more easier to read, as following :

DAO layer


package com.mkyong.customer.dao;

import org.springframework.stereotype.Repository;

@Repository
public class CustomerDAO 
{
	@Override
	public String toString() {
		return "Hello , This is CustomerDAO";
	}	
}

Service layer


package com.mkyong.customer.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mkyong.customer.dao.CustomerDAO;

@Service
public class CustomerService 
{
	@Autowired
	CustomerDAO customerDAO;

	@Override
	public String toString() {
		return "CustomerService [customerDAO=" + customerDAO + "]";
	}
		
}

Download Source Code

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
56 Comment threads
8 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
58 Comment authors
SagarCroBoyarun singhSavitaKarthikeyan Recent comment authors
newest oldest most voted
Indra Eka Prasetya
Guest
Indra Eka Prasetya

Why do we still use CustomerService cust = (CustomerService)context.getBean(“customerService”); ? and not use @Autowired for CustomerService

Cor
Guest
Cor

Great tutorial, really clear and step by step.

I might add that if anyone is using JDK 1.8 and is getting the error “component-scan and its parser class are only available on JDK 1.5 and higher” after enabling the component-scan you might need to switch to JDK 1.7 as spring-context-2.5 and JDK 1.8 are apparently not compatible anymore.

trackback
Spring Auto Components Scanning with Constructor injection | Technology & Programming

[…] know how to use Auto Components Scanning and Consctuctor Injection individually. http://www.mkyong.com/spring/spring-auto-scanning-components/ […]

Landscape
Guest
Landscape

NIce post, easy to understand.

atul galande
Guest
atul galande

Which stereo type annotation used in web mvc module

Savita
Guest
Savita

Great explanation!! Your posts are really very useful and easy to understand. Thank you so much for your efforts.

trackback
Spring – Filtering Components in Auto Scanning | Spring

[…] Components in Auto Scanning Written on March 25, 2010 at 5:25 am by mkyong In last tutorial, you learn about how to enable the auto scan components with @Component annotation. The alternative […]

Noman
Guest
Noman

hmmmm nice great post….

trackback
Spring Tutorials | J2EE Web Development Tutorials

[…] Auto Scanning Components How to automatic scan, detect and instantiate your beans or components from project class path. […]

trackback
Spring MVC hello world annotation example

[…] />   </beans> Note You may interest at this Spring auto scanning components […]

anil
Guest
anil

What about perfomance issues with Auto Scanning Component compared to Manual Components Declaration ?

Dhafir
Guest
Dhafir

Brilliant article! Easy and straightforward.
Thank you!

trackback
Jersey + Spring integration example

[…] Jersey-Spring-Integration-Example.zip (8 KB)ReferencesJersey Spring JavaDocSpring 3 in JerseySpring auto component scanningRESTEasy + Spring integration example […]

alex
Guest
alex

thx ! this article easy to understand, really great

Alebald
Guest
Alebald

Could you please tell me, how to use Spring Auto Scanning
for multiple path. say i have my Action Objects at “com.personal.server.handler” and
Dao at “com.personal.server.dao.handler”. Sould i use two explicit tag in my configuration.xml ?

vijay
Guest
vijay

Can you explain why you don’t need to Include in the configuration file as explained in your other Tuturial ?

regards,
vijay

Borgy Manotoy
Guest
Borgy Manotoy

I have a related question regarding autowiring 🙂 I created a new controller, MyController.java… I wanted to autowire some service interfaces (AccountService, CompetitionService). In my application, there are already other existing controllers (i.e InvitationController.java, CompetitionController.java, etc) which does not have any autowire issues. all of them belongs to the same package (i.e. com.test.app), I also added the line below in the XML file. now, I really do not know why I am having difficulties in autowiring the service interfaces in the newly created controller. I tried almost copying the code of the most simple existing controller but still I have… Read more »

Levan
Guest
Levan

Thanks…

Nitesh
Guest
Nitesh

simple and easy..
thanx

Pritish
Guest
Pritish

Excellent post !

ahmet
Guest
ahmet

why do we use @Override ?

Sharath
Guest
Sharath

Thanks a lot mkYong.. the above post gives good understanding.

bruno
Guest
bruno

Thank you for this post, clear and objective.

Saravanan
Guest
Saravanan

Awesome… I have used this concept in my project, but haven’t understand clearly. After reading this article got clear concept… Thanks

Alex
Guest
Alex

Thank you for your posts. Your English is not the best and really frustrating but your excellent tutorials make up for it. Keep up the great work!

Peter Jerald
Guest
Peter Jerald

Thanks bro, your articles saved me in various situations, Please keep doing the same.

devi
Guest
devi

great Article

Ali Kemal
Guest
Ali Kemal

Thanks 🙂

Kumaran
Guest
Kumaran

crystal clear…

Mahender
Guest
Mahender

your superb…….