Spring notes and usage

@Configuration

explain

This is equivalent to using this class as the beans tag in Spring's XML configuration file, indicating that this is a configuration class

usage

Put the annotation on top of the class

package com.ledao.config;

import org.springframework.context.annotation.Configuration;

/**
 * @author LeDao
 * @company
 * @create 2022-02-15 20:39
 */
@Configuration
public class MyConfig {
}

@Bean

explain

The bean tag in the XML configuration file equivalent to Spring is used to register bean objects

usage

If the name attribute is not defined, the id of the bean is the method name, and if the name attribute is defined, the id of the bean is the attribute value of name (in this case, the method name cannot be used); The name of this method is equivalent to the id attribute in the bean tag, and the return value of this method is equivalent to the class attribute in the bean tag

package com.ledao.config;

import com.ledao.entity.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author LeDao
 * @company
 * @create 2022-02-15 20:39
 */
@Configuration
public class MyConfig {

    @Bean
    public Student student(){
        Student student = new Student();
        student.setName("tom");
        return student;
    }
}

@Component

explain

It is used to register the bean object, which is equivalent to the bean tag of the XML configuration file

usage

Put the annotation on top of the class

package com.ledao.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author LeDao
 * @company
 * @create 2022-02-15 20:39
 */
@Component
public class Student {

    @Value(value = "tom")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}

@Repository

explain

It is used to register bean objects, which is equivalent to the bean tag of XML configuration file. It has the same effect as @ Component and corresponds to the persistence layer (i.e. the implementation class of dao interface). It is mainly used for database related operations

usage

Use this annotation on the implementation class of dao interface

package com.ledao.dao;

import org.springframework.stereotype.Repository;

/**
 * @author LeDao
 * @company
 * @create 2022-02-16 21:31
 */
@Repository(value = "userDao")
public class UserDaoImpl implements UserDao{

    @Override
    public void add() {
        System.out.println("Add user");
    }

    @Override
    public void delete() {
        System.out.println("delete user");
    }

    @Override
    public void update() {
        System.out.println("Modify user");
    }

    @Override
    public void findById() {
        System.out.println("according to id Find users");
    }
}

@Service

explain

It is used to register bean objects, which is equivalent to the bean tag of the XML configuration file. It has the same effect as @ Component and corresponds to the Service layer (i.e. the implementation class of the Service interface). It is mainly used to design some complex logic and needs to use the dao layer

usage

package com.ledao.service.impl;

import com.ledao.dao.UserDao;
import com.ledao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author LeDao
 * @company
 * @create 2022-02-16 21:04
 */
@Service(value = "userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public void add() {
        userDao.add();
    }

    @Override
    public void delete() {
        userDao.delete();
    }

    @Override
    public void update() {
        userDao.update();
    }

    @Override
    public void findById() {
        userDao.findById();
    }
}

@Controller

explain

It is used to register bean objects, which is equivalent to the bean tag of XML configuration file. It has the same effect as @ Component and corresponds to the Spring MVC control layer. It is mainly used to receive user requests and call the Service layer to return data to the front-end page

usage

package com.ledao.controller.admin;

import com.ledao.entity.BookType;
import com.ledao.entity.PageBean;
import com.ledao.service.BookService;
import com.ledao.service.BookTypeService;
import com.ledao.util.StringUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Background book category Controller layer
 *
 * @author LeDao
 * @company
 * @create 2022-01-21 23:57
 */
@RestController
@RequestMapping("/admin/bookType")
public class BookTypeAdminController {

    @Resource
    private BookTypeService bookTypeService;

    /**
     * Check whether the book category id exists
     *
     * @param bookTypeId
     * @return
     */
    @RequestMapping("/checkBookTypeId")
    public Map<String, Object> checkBookTypeId(@RequestParam(value = "bookTypeId", required = false) Integer bookTypeId) {
        Map<String, Object> resultMap = new HashMap<>(16);
        BookType bookType = bookTypeService.findById(bookTypeId);
        if (bookType == null) {
            resultMap.put("success", false);
            resultMap.put("errorInfo", "The book category you selected does not exist,Please reselect!!");
        } else {
            resultMap.put("success", true);
        }
        return resultMap;
    }
}

@Value

explain

Values used to inject attributes

usage

Put the annotation on the attribute defined by the entity class or on the Setter method. If the annotation is used in both places, the Setter method shall prevail

@Value(value = "tom")
private String name;

@Autowired and @ Resource

These two annotations are used to realize the automatic assembly of Spring. See the blog for details and usage: Using annotations to realize Spring automatic assembly | LeDao's blog (zoutl.cn)

@Scope

explain

Define the scope of a method using @ bean annotation under the configuration class

usage

The default scope of spring beans is singleton

When using this annotation on a method using @ bean annotation, the scope of the method is defined, which is only valid for the method

package com.ledao.config;

import com.ledao.entity.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * @author LeDao
 * @company
 * @create 2022-02-15 20:39
 */
@Configuration
public class MyConfig {

    @Bean
    @Scope(value = "prototype")
    public Student student(){
        return new Student();
    }
}

@ComponentScan

explain

It is used to define the scanning path and find out the classes that need to be assembled from it. It is automatically assembled into the bean container of spring (classes annotated with @ Component)

usage

It is generally used on the configuration class. Scan the classes under the specified package and assemble all the classes that need to be assembled into the bean container of spring; If you do not use this annotation, you will only assemble the class corresponding to the method using @ bean under the configuration class

package com.ledao.config;

import com.ledao.entity.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author LeDao
 * @company
 * @create 2022-02-15 20:39
 */
@Configuration
@ComponentScan("com.ledao.entity")
public class MyConfig {

    @Bean
    public Student student(){
        return new Student();
    }
}

If you want to scan multiple packages, the usage is as follows: (you can also use @ ComponentScans annotation)

@ComponentScan(value = {"com.ledao.entity","com.ledao.service.impl"})

@ComponentScans

explain

The same effect as @ ComponentScan is used to scan multiple packages

usage

@ComponentScans(value = {@ComponentScan(value = "com.ledao.entity"), @ComponentScan(value = "com.ledao.service.impl")})

@Import

explain

Configuration for importing other configuration classes

usage

Use @ Import() on the configuration class to import other configuration classes, and fill in the name of the configuration class to import in parentheses class

package com.ledao.config;

import com.ledao.entity.Student;
import org.springframework.context.annotation.*;

/**
 * @author LeDao
 * @company
 * @create 2022-02-15 20:39
 */
@Configuration
@Import(MyConfig2.class)
public class MyConfig {

    @Bean
    public Student student(){
        return new Student();
    }
}

@Aspect

explain

The class used to annotate the annotation is a facet

usage

View blog: Spring uses annotations to implement AOP | LeDao's blog (zoutl.cn)

@Before

explain

Execute this method before executing business code

usage

View blog: Spring uses annotations to implement AOP | LeDao's blog (zoutl.cn)

@After

explain

Execute this method after executing the business code

usage

View blog: Spring uses annotations to implement AOP | LeDao's blog (zoutl.cn)

@Around

explain

Use of surround enhancement method

usage

View blog: Spring uses annotations to implement AOP | LeDao's blog (zoutl.cn)

@EnableAspectJAutoProxy

explain

Start AspectJ automatic agent

usage

View blog: [Spring implements AOP with annotations | Ledao's blog (zoutl.cn)](

Keywords: Java Spring Back-end

Added by DoctorT on Sat, 19 Feb 2022 16:39:27 +0200