Analysis of key and difficult points of spring AOP

Time:2021-4-30

Analysis of key and difficult points

What is AOP, what problems are solved, and application scenarios?

What is it?

AOPAspect oriented programming can enhance the function of the code without changing the original code.

What problems have been solved?

AOP is mainly used to solve: without changing the original business logic, enhance the crosscutting logic code, fundamentally decouple, and avoid the duplication of crosscutting logic code.
Can solveBloated codeTo improve the efficiency of the codeMaintainability

Application scenarios?

Mainly used in: logging, transaction control, performance statistics, security control, exception handling, etc.

AOP programming basic steps and implementation process (based on AspectJ framework for example).

1. Add dependency

<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2. Create log facet class object (take log as an example)

package com.cy.pj.common.aspect;
@Aspect // indicates that such an object is a section
@Slf4j
@Component // give this class to spring management
public class SysLogAspect {
         @Pointcut ("bean (sysuserserviceimpl)") // pointcut
         public void logPointCut() {}
         @Around ("logpointcut()") // surround notification, indicating that this method is executed before and after the execution of core business
         public Object around(ProceedingJoinPoint jp)
         throws Throwable{
                 try {
                   log.info("start:{}"+System.currentTimeMillis());
                   Object result=jp.proceed();// The target method is eventually called
                   log.info("after:{}"+System.currentTimeMillis());
                   return result;
                 }catch(Throwable e) {
                   log.error("after:{}",e.getMessage());
                   throw e;
                 }
         }
}

3. Testing

@SpringBootTest
public class AopTests {
         @Autowired
         private SysUserService userService;
         @Test
         public void testSysUserService() {   PageObject<SysUserDeptVo>po=userService.findPageObjects("admin",1);
          System.out.println("rowCount:"+po.getRowCount());
         }
}

The core object and application relationship in AOP programming.

Aspect: aspect, a module object composed of a series of tangent points, enhancements and introductions, which can define priorities, thus affecting the execution order of enhancements and introductions. Transaction management is a good example in Java enterprise application.

Join pointAccess point, a point during the execution of a program, such as method execution, class initialization, and exception handling. In spring AOP, access points always represent method execution.

Advice: enhancement, the execution of section at a specific access point, including “around”, “before” and “after”. Many AOP frameworks, including spring, usually use interceptors to enhance, maintaining an interceptor chain around the access point.

PointcutPointcut: a predicate (expression) used to match a specific access point. The enhancement will be associated with the pointcut expression and run on any access point to which the pointcut matches. Matching access points through tangent expressions is the core of AOP, and spring uses AspectJ’s tangent expressions by default.

Introduction: to declare additional methods and fields for a type. Spring AOP allows you to introduce any interface and its default implementation to the enhanced object.
Target object: target object, an object enhanced by one or more facets. Also called enhanced objects. Since spring AOP uses runtime proxies, the target object is always a proxy object.

AOP proxy: AOP proxy, in order to realize the section function, an object will be created by AOP framework. In the spring framework, the default way of AOP proxy is: if there is an interface, the JDK dynamic proxy based on the interface is used, otherwise the cglib dynamic proxy based on the class is used. But we can use cglib dynamic proxy completely by setting proxy target class = true.

Weaving: weaving, linking one or more facets to a class or object to create an enhanced object. Weaving can occur at compile time (using the AspectJ compiler), load time, or runtime. Spring AOP is run-time weaving by default, which can be set by enumerating advicemode.

Analysis of the implementation principle of AOP in spring.
The implementation of configuration based on annotation in AOP [email protected] Aspect,@PointCut ,@Around ,…)
Transaction control based on annotation in AOP [email protected] Transactional)
Implementation of asynchronous operation in AOP programming (@ EnableAsync,@Async )
Cache application in AOP programming (@ EnableCaching,@Cacheable ,@CacheEvict )

FAQ analysis

What is the OCP principle?
What is the dip principle?
What is the single responsibility principle (SRP)?
What are the configuration methods of AOP in spring( XML, annotations)
What are the basic types of AOP notification in spring( 5 species)
How does AOP create proxy objects for bean objects in spring( JDK,CGLIB)
How to specify the execution order of AOP facets in spring (@ Order)
Transaction control in spring monomer architecture project should be realized through connection object,?
How does spring guarantee one thread and one connection object? With the help of ThreadLocal?
What problems may occur when multiple transactions are executed concurrently( Dirty reading, unrepeatable reading, phantom reading)
Do you know the isolation level of transactions? Do you know the specific application scenarios?

Recommended Today

The use of springboot Ajax

Ajax overview What is Ajax? data Ajax application scenarios? project Commodity system. Evaluation system. Map system. ….. Ajax can only send and retrieve the necessary data to the server, and use JavaScript to process the response from the server on the client side. data But Ajax technology also has disadvantages, the biggest disadvantage is that […]