Besides define Spring beans in configuration file, Spring also provide 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.
[email protected], @Repository, @Service and @Controller Annotations Introduction.
- @Component : This is the base annotation, it is used to annotate other annotation such as @Repository, @Service and @Controller. You can see this by annotation source code as below. Annotated by @Component means this class or interface is a Spring managed bean.
- @Repository : This is used to annotate a special type bean, such as Data Access Object (DAO). Annotated with @Repository, Spring framework will treat it as a DAO bean to implement database operations.
- @Service : If your bean provide service function for example business logic method, then you can use @Service to annotate it. Generally you will invoke DAO beans method in @Service bean method.
- @Controller : This annotate a class as a controller bean. A controller usually handle client request, check which service bean can handle it. then it will pass client request data to the service object to operate. This is usually used in Spring MVC.
- When you use above annotation, you can give the annotated bean an id, for example
@Controller("requestConroller")
. Then you can useApplicationContext.getBean("requestConroller")
to retrieve it.
2. @Repository Example.
BankCardDAO.java : This is the interface which Spring bean will implements. Use interface we can create more than one implementation classes, and reference all 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 application context configuration file.
You just need to add context:component-scan tag, 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" />
Example Output.
[download id=”2926″]