Annotation based assembly

Keywords: Java Spring Spring Boot Java framework

Common annotations in Spring are as follows.


     This annotation can be used to describe beans in Spring, but it is a generalized concept that represents only one component (Bean) and can act at any level. When using, you only need to mark the annotation on the corresponding class.
       For better layering, Spring can use the other three annotations with the same functions. At present, which function is used is the same.
        @ Controller: web layer
        @ Service: service layer
        @ Repository: dao layer
         Writing these annotations is equivalent to giving this class to the Spring management assembly!  


     The class used to identify the data access layer (DAO layer) as a Bean in Spring has the same function as @ Component.


     It usually acts on the business layer (Service layer) to identify the class of the business layer as a Bean in Spring, and its function is the same as @ Component.


     It usually acts on the control layer (such as the Action of struts 2 and the Controller of Spring MVC). It is used to identify the class of the control layer as a Bean in Spring. Its function is the same as that of     @ Same as Component.


         It can be applied to Bean's attribute variables, attribute setter methods, non setter methods and constructors, and cooperate with the corresponding annotation processor to complete the automatic configuration of Bean. By default, it is assembled according to the type of Bean.


         The function is the same as that of Autowired, except that @ Autowired is assembled according to the Bean type by default, while @ Resource is assembled according to the Bean instance name by default.
        @ There are two important attributes in a Resource: name and type.
     Spring resolves the name attribute to the instance name of the Bean, and the type attribute to the instance type of the Bean. If the name attribute is specified, the assembly is performed by the instance name; If the type attribute is specified, the assembly is performed by Bean type. If none is specified, assemble according to the Bean instance name first. If it cannot match, assemble according to the Bean type again; If none of them can match, a NoSuchBeanDefinitionException is thrown.


     When used with @ Autowired annotation, the default assembly by Bean type will be modified to assembly by Bean instance name, which is specified by the parameter of @ Qualifier annotation.

Code example:

Dao layer:

package com.openlab.Dao;

public interface stuDao {
   public void  out();
DaoImpl layer
package com.openlab.Dao.Impl;

import com.openlab.Dao.stuDao;
import org.springframework.stereotype.Repository;

public class StuImpl implements stuDao {

    public void out() {
        System.out.println("add method=====");


package com.openlab.Service;

public interface StuService {
    public void out();

Service implementation class

import javax.annotation.Resource;

public class StuServiceImpl implements StuService{
    private stuDao stuDao;

    public stuDao getStuDao() {
        return stuDao;

    public void setStuDao(stuDao stuDao) {
        this.stuDao = stuDao;

    public void out() {
        System.out.println("service Method execution--------");

Controller layer

package com.openlab.Controller;

import com.openlab.Service.StuService;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

public class StuController {
    private StuService stuservice;

    public StuService getStuservice() {
        return stuservice;

    public void setStuservice(StuService stuservice) {
        this.stuservice = stuservice;

    public void out() {


  The results are as follows:


Posted by yogadt on Fri, 17 Sep 2021 21:08:03 -0700