Annotation based assembly

Keywords: Java Spring Spring Boot Java framework

Common annotations in Spring are as follows.

    1)@Component

     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!  

    2)@Repository

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

    3)@Service

     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.

    4)@Controller

     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.

    5)@Autowired

         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.

    6)@Resource

         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.

    7)@Qualifier

     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;

@Repository("stuDao")
public class StuImpl implements stuDao {


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

Service

package com.openlab.Service;


public interface StuService {
    public void out();
}

Service implementation class

import javax.annotation.Resource;

@Service("stuservice")
public class StuServiceImpl implements StuService{
    @Resource(name="stuDao")
    private stuDao stuDao;

    public stuDao getStuDao() {
        return stuDao;
    }

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

    @Override
    public void out() {
        stuDao.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;

@Controller("StuController")
public class StuController {
    @Resource(name="stuservice")
    private StuService stuservice;

    public StuService getStuservice() {
        return stuservice;
    }

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

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

Test:

  The results are as follows:

 

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