Spring Autowire Use @Component, @Repository, @Service and @Controller Annotations

Besides define Spring beans in a configuration file, Spring also provides some java annotation interface for you to make Spring bean declaration simple and easy. These interfaces are also called stereotype annotation. They are @Component, @Repository, @Service, and @Controller. If you want to reference such a bean, you just need to annotate that field with @Autowired annotation. Then Spring framework will automatically inject dependency beans.

1.@Component, @Repository, @Service, @Controller Annotations Introduction.

  1. @Component: This is the base annotation, it is used to annotate other annotations such as @Repository, @Service, @Controller. If a java class or interface is annotated by @Component means this class or interface is a Spring-managed bean.
  2. @Repository: This is used to annotate a special type of bean, such as Data Access Object (DAO). Annotated with @Repository, the Spring framework will treat it as a DAO bean to implement database operations.
  3. @Service: If your bean provides a service function for example business logic method, then you can use @Service to annotate it. Generally, you will invoke the DAO beans method in the @Service bean method.
  4. @Controller: This annotates a class as a controller bean. A controller usually handles client requests, check which service bean can handle the requests. then it will pass client request data to the service object to process. This is usually used in Spring MVC.
  5. When you use the above annotation, you can give the annotated bean an id. For example, @Controller("requestConroller") annotated the beans with the id requestController. Then you can use the code ApplicationContext.getBean("requestConroller") to retrieve the controller bean.

2. @Repository Example.

BankCardDAO.java: This is the interface which Spring bean will implements. Use interface we can create more than one implementation class and reference all of them by one interface class.

public interface BankCardDAO {
	
	public BankCardDTO insertBankCard(String bankName, String cardNumber, String createDate);

}

BankCardDAOImpl.java

// Annotated this object as a DAO function bean.
@Repository("bcDao")
public class BankCardDAOImpl implements BankCardDAO {

	@Override
	public BankCardDTO insertBankCard(String bankName, String cardNumber, String createDate) {
		BankCardDTO ret = new BankCardDTO();
		ret.setBankName(bankName);
		ret.setCardNumber(cardNumber);
		ret.setCreateDate(createDate);
		
		System.out.println("Bank card has been inserted by BankCardDAOImpl. Bank name : " + bankName + " , card number : " + cardNumber + " , create date : " + createDate);
		return ret;
	}

}

3. @Service Example.

BankCardManager.java

public interface BankCardManager {

	public BankCardDTO createBankCard(String bankName, String cardNumber, String createDate);
	
}

BankCardManagerImpl.java

// Annotated this object as a service bean.
@Service("bcManager")
public class BankCardManagerImpl implements BankCardManager {

	@Autowired
	private BankCardDAO bankCardDao;
	
	@Override
	public BankCardDTO createBankCard(String bankName, String cardNumber, String createDate) {
		
		System.out.println("Bank card has been created by BankCardManagerImpl. Bank name : " + bankName + " , card number : " + cardNumber + " , create date : " + createDate);

		return this.bankCardDao.insertBankCard(bankName, cardNumber, createDate);
	}

}

4. @Controller Example.

BankCardController.java

// Annotated this object as a controller bean.
@Controller("bcController")
public class BankCardController {
	@Autowired
	private BankCardManager bcManager;
	
	public BankCardDTO createBankCard(String bankName, String cardNumber, String createDate)
	{
		return this.bcManager.createBankCard(bankName, cardNumber, createDate);
	}

}

5. Test Application And Bean Context Configuration File.

TestAutowireUseAnnotation.java

public class TestAutowireUseAnnotation {

	public static void main(String[] args) {
	   	// Initiate Spring application context.
		ApplicationContext springAppCtx = new ClassPathXmlApplicationContext("AutowireByAnnotationBeanSettings.xml");

                // Get @Controller annotated bean by id.
		BankCardController bcController = (BankCardController)springAppCtx.getBean("bcController");
		
		bcController.createBankCard("Bank Of China", "BOC888888", "2017/08/08");
	}

}

AutowireByAnnotationBeanSettings.xml: Use @Repository, @Service, and @Controller annotation, you do not need to define beans in the application context configuration XML file. You just need to add context:component-scan tag in the XML file, the base-package attribute’s value is the package name where only all classes under that package need to be scanned.

<context:component-scan base-package="com.dev2qa.example.spring.bean.autowire.annotation" />

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.