By default, looks for classes that are annotated with one of the following sterotype annotations: 

  • @Component - A general-purpose stereotype annotation indicating that the class is a Spring component.
  • @Controller - Indicates that the class defines a Spring MVC controller
  • @Repository - Indicates that the class defines a data repository (=DAO)
  • @Service - Indicates that the class defines a service




Maven configuration


We need to add the spring.jar and spring-context.jar to our classpath.




Repository

package org.camelcode.repository;

import org.springframework.stereotype.Repository;

@Repository
public class DancerDao {

	public String doDance() {
		return "Dancing...";
	}
}


Service


@Service means that this class is a service. This can also be @Component or the other annotations. The difference between the annotations is just a naming convention. A quick glance at what type of bean this is.


package org.camelcode.service;

import org.camelcode.repository.DancerDao;
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();
    }
}


Application context


The <context:component-scan /> element does everything that <context:annotation-config /> does and more. It configures Spring to automatically discover beans and declare them for you. So most (or all) of your beans in your Spring application can be declared and wired without using <bean>.

The <ontext:component-scan/> element workds by scanning a package and all of its child packages, looking for classes with the right annotations. The 'base-package' attribute tells <context:component-scan/> the package to start its scan from.




References