[java spring] annotation automatic assembly

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, which only represents a component (Bean) and can act at any level. When using, you only need to mark the annotation on the corresponding class.
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, and its function is the 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.

@The four annotations of Component, @ Repository, @ service and @ Controller actually have the same function. The only difference is that the semantics are different. Like the service layer, we usually use @ service. Of course, it's OK to replace it with the other three, but it's more meaningful.

Test code:

package upc.yqs.dao;

public interface UserDao
{
    void showData();
}

package upc.yqs.dao.impl;

import org.springframework.stereotype.Repository;
import upc.yqs.dao.UserDao;

@Repository("userDao")
public class UserDaoImpl implements UserDao {
    @Override
    public void showData() {
        System.out.println("dao Layer called");
    }
}
package upc.yqs.service;

public interface UserService
{
    void showData();
}
package upc.yqs.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import upc.yqs.dao.UserDao;
import upc.yqs.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService
{
    @Autowired
    @Qualifier("userDao")
    public UserDao userDao;
    @Override
    public void showData() {
        System.out.println("service Layer called");
        userDao.showData();
    }
}
package upc.yqs.controller;

public interface UserController
{
    void showData();
}
package upc.yqs.controller.impl;

import org.springframework.stereotype.Controller;
import upc.yqs.controller.UserController;
import upc.yqs.service.UserService;

import javax.annotation.Resource;

@Controller("userController")
public class UserControllerImpl implements UserController
{
    @Resource(name = "userService")
    private UserService userService;

    @Override
    public void showData() {
        System.out.println("controller Layer called");
        userService.showData();
    }
}

Test method:

    public static void main(String[] args)
    {
        ApplicationContext context= new ClassPathXmlApplicationContext("bean.xml");
        UserController userController=(UserController) context.getBean("userController");
        System.out.println("main Method called");
        userController.showData();
    }

Of course, we need to define a bean before testing xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan
            base-package="upc.yqs" />
</beans>

We scanned UPC. xml All classes under YQS, so that our annotated attributes and classes will be automatically assembled into this xml, and then we can create a bean container to get the beans.

Output:

main Method called
controller Layer called
service Layer called
dao Layer called

Keywords: Java Spring Back-end

Added by SueHubert on Tue, 21 Dec 2021 13:47:18 +0200