Share on Google+Share on Google+

@Component Annotation in Spring, Spring Autoscan

In this tutorial you will learn about auto scanning feature of Spring framework.

@Component Annotation in Spring

In Spring normally if there is bean we need to define in the bean configuration file. If you have lot many bean file it is tedious to define all bean in xml configuration so to overcome from this you can scan all your bean through spring auto scan feature. The @Component in StudentDAO class indicates that it is an auto scan component. The @Autowired annotation tell to auto wire the student DAO by type.

package com.roseindia.student.dao;

import org.springframework.stereotype.Component;

public class StudentDAO {
	public String toString() {
		return " inside StudentDAO";


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

import com.roseindia.student.dao.StudentDAO;

public class StudentService {
	StudentDAO studentDAO;

	public String toString() {
		return "StudentService [studentDAO=" + studentDAO + "]";

package com.roseindia.common;

import org.springframework.context.ApplicationContext;


public class MainApp {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "context.xml" });
		StudentService stud = (StudentService) context


<beans xmlns=""
  <context:component-scan base-package="com.roseindia.student" />

When you run this application it will display message as shown below:

StudentService [studentDAO= inside StudentDAO]

Download this example code


Posted on: September 2, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.