Common knowledge accumulation of spring boot development

Article catalog

SpringBoot get Request and Response

Through static method acquisition, you can also encapsulate a static method

public String test() {
	ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
	HttpServletRequest request = attributes.getRequest();
	HttpServletResponse response = attributes.getResponse();


Instance, log facet to obtain request information:

Get directly through parameters
As long as you add parameters to your methods, Springboot will help you bind, and you can use it directly. If your method has other parameters, add these two to the end.

stay Controller In the method

@RequstMapping(value = "")
public String test(HttpServletRequest request,HttpServletResponse response) {

Inject into class
, so that you don't have to write every method

private HttpServletRequest request;
private HttpServletResponse response;
@GetMapping(value = "")
public String test() {

Transaction uses @ Transactional

AOP development process

Aop (aspect oriented programming): aspect oriented programming, which separates general logic from business logic. Aop divides the software system into two parts: core concerns and crosscutting concerns. The main process of business processing is the core concern, and the little related part is the crosscutting concern. One of the characteristics of crosscutting concerns is that they often happen in many places of core concerns, and they are basically similar everywhere. For example, authority authentication, log, transaction processing. The role of Aop is to separate the various concerns in the system and separate the core concerns from the crosscutting concerns. As Adam Magee, a senior solution architect at Avanade, said, the core idea of Aop is to "separate the business logic in the application from the common services that support it".

Related concepts:
Join point: refers to the extension point that needs to insert crosscutting concerns in the program. The join point may be class initialization, method execution, method call, field call or exception handling, etc. Spring only supports method execution join point; in AOP, it means "where to do";

Pointcut: select a set of related connection point patterns, that is, the set of connection points. Spring supports perl5 regular expressions and AspectJ pointcut patterns. By default, spring uses AspectJ syntax; in AOP, it is expressed as "the set where to do";

Advice: the behavior performed on the connection point. The Notice provides the means to extend the existing behavior at the connection point selected by the entry point in AOP, including before advice, after advice, and around advice Advice), which implements AOP through proxy mode in Spring, and weaves notifications through interceptor mode through interceptor chain around connection point; in AOP, it means "what to do";

Aspect: modularization of crosscutting concerns, such as log components. It can be regarded as a combination of notification, introduction and pointcut; in Spring, it can be implemented in the form of Schema and @ AspectJ; in AOP, it is expressed as "where to do and what to do collection";
Introduction: also known as internal type declaration, it adds new fields or methods to existing classes. Spring allows the introduction of new interfaces (which must correspond to one implementation) to all the proxied objects (target objects); in AOP, it is expressed as "what to do (what to introduce)";
Target Object: the object that needs to be woven into the crosscutting concerns, that is, the object selected by the pointcut and the object to be notified, which can also be called "notified object"; because Spring AOP is implemented through the proxy mode, the object is always the proxy object; in AOP, it is expressed as "to whom";
AOP Proxy: the AOP framework uses the object created by proxy mode to insert a notification (i.e. apply facet) at the connection point, that is, apply facet to the target object through proxy. In Spring, AOP Proxy can be implemented by JDK dynamic proxy or CGLIB proxy, and facet can be applied by interceptor model.
Weaving: weaving is a process in which facets are applied to target objects to create AOP proxy objects. Weaving can be performed during compilation, class loading, and runtime. Assemble the aspect to create a notified object. This can be done at compile time (using the AspectJ compiler, for example), or at run time. Spring, like other pure Java AOP frameworks, does weaving at run time.

Aop common concepts:

concept explain
Tangent point The place to add code is called the pointcut
Notification (enhanced) Notification is code that is added dynamically to the pointcut
section Cut point + notice
Connection point Definition of tangent point

Development steps:
The first step is to define a facet class @ Aspect
Step 2: define a PointCut method @ PointCut, @ PointCut annotation indicates the point of entry, that is, the general logic business in the program. Here is the path of the request
The third step is based on how to operate @ Before and After the pointcut business processing
Log development example:

package com.example.aspect;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.JoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
public class HttpAspect {
    private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);
    //@Before annotation is executed before method execution
    //Intercept the studentList under this path()Method, two points for any parameter
//    @Before("execution(public * com.example.controller.StudentController.studentList(..))")
//    public void log(){
//        System.out.println("Test before");
//    }
//    @After("execution(public * com.example.controller.StudentController.studentList(..))")
//    public void doAfter(){
//        System.out.println("Test after");
//    }
    //Define a common method
    @Pointcut("execution(public * com.example.controller.StudentController.*(..))")
    public void log(){
    public void doBefore(JoinPoint joinPoint){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //url"url={}", request.getRequestURI());
        //method"method={}", request.getMethod());
        //ip"ip={}", request.getRemoteAddr());
        //method"class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        //param"args={}", joinPoint.getArgs());
    public void doAfter(){
    @AfterReturning(returning="obj", pointcut = "log()")
    public void doAfterReturnig(Object obj){"reponse={}", obj);

Reference article:

Tags: Spring SpringBoot Programming JDK

Posted on Thu, 25 Jun 2020 06:50:11 -0400 by expert_21