Summary of knowledge points of spring framework AOP implementation

 

Summary of knowledge points of spring framework AOP implementation

1. AOP core components

1.1. Aspect: A class is an abstraction of the characteristics of an object, and an aspect is an abstraction of cross-cutting concerns

1.2. Joinpoint: The intercepted method, field or constructor

1.3. Pointcut: Definition of Intercepting Connection Points

1.4. Advice: code to be executed after intercepting a join point

1.5. target: the target object of the proxy

2. 5 types of enhanced processing of AOP

3. Use XML configuration to achieve pre- and post-enhanced logging

3.1. Using jar packages

3.2. Defining business methods

3.3. Defining Enhanced Classes

3.4. Configure the log4J resource file:

3.4.1. Set the logging level (fatal>error>warn>info>debug)

3.4.2. Set the destination of the log output (console, file)

3.4.3. Formatting log output

3.4.4. If using file output, you need to set the file name of the log file

3.5. Configure spring's xml file

3.5.1. Injecting business objects

3.5.2. Injecting the enhanced object that completes the cut-in

3.5.3. Setting up AOP

3.5.3.1. Set the pointcut pointcut: the scope of the object being cut into

3.5.3.2. Execute expression: a scope expression that specifies the method to be cut in

//All methods of all classes ending with Service under the com.service package

execution(* com.service.*Service.*(..))

3.5.3.3. Set the aspect (aspect): specify the method of the enhanced class that completes the pointcut operation, and associate the corresponding pointcut

 

ref The id of the object bean that completes the aspect

pointcut-ref specifies the target position to be cut in, the pointcut

-->

 

 

 

4. Surround enhancement

4.1. Defining Enhanced Classes

package com.aop;

import java.util.Arrays;

import org.apache.log4j.Logger;

import org.aspectj.lang.ProceedingJoinPoint;

//环绕增强类:实现全程监控被切入方法的运行状态

public class AroundLogger {

Logger logger=Logger.getLogger(AroundLogger.class);

/**

* 在目标对象的方法执行前后进行增强操作,如果目标方法发生异常,由增强方法来处理

* @param point 连接点对象:拥有被切入对象的信息

* @return 被切入方法的返回值

*/

public Object doListen(ProceedingJoinPoint point){

logger.info(point.getTarget()+"对象的"+point.getSignature().getName()

+"方法即将执行,方法参数列表为:"+Arrays.toString(point.getArgs()));

Object result=null;

try {

//调用连接点对象的进程方法,让目标对象的业务方法执行

//该方法返回的是目标对象的被切入方法的返回值

result= point.proceed();

} catch (Throwable e) {

e.printStackTrace();

logger.info(point.getTarget()+"对象的"+point.getSignature().getName()

+"方法发生异常!异常信息为:"+e.getMessage());

result=0;

}finally{

logger.info(point.getTarget()+"对象的"+point.getSignature().getName()

+"方法执行完毕,方法参数列表为:"+Arrays.toString(point.getArgs())+";目标方法的返回值为"+result);

}

return result;

}

}

4.2. 在spring配置文件中注入需要被增强的业务对象,以及环绕增强类对象

 

 

 

 

 

 

 

 

ref 完成切面的对象bean的id

pointcut-ref 指定需要切入的目标位置,切入点

-->

 

 

5. 使用注解实现AOP

5.1. 定义注解方式的增强类

//环绕增强类:实现全程监控被切入方法的运行状态

//@Aspect 当前类为增强类

@Aspect

public class AroundLogger2 {

Logger logger=Logger.getLogger(AroundLogger2.class);

/**

* 在目标对象的方法执行前后进行增强操作,如果目标方法发生异常,由增强方法来处理

* @param point 连接点对象:拥有被切入对象的信息

* @return 被切入方法的返回值

*/

@Around(value="execution(* com.service.*Service.*(..))")

public Object doListen(ProceedingJoinPoint point){

logger.info(point.getTarget()+"对象的"+point.getSignature().getName()

+"方法即将执行,方法参数列表为:"+Arrays.toString(point.getArgs()));

Object result=null;

try {

//调用连接点对象的进程方法,让目标对象的业务方法执行

//该方法返回的是目标对象的被切入方法的返回值

result= point.proceed();

} catch (Throwable e) {

e.printStackTrace();

logger.info(point.getTarget()+"对象的"+point.getSignature().getName()

+"方法发生异常!异常信息为:"+e.getMessage());

result=0;

}finally{

logger.info(point.getTarget()+"对象的"+point.getSignature().getName()

+"方法执行完毕,方法参数列表为:"+Arrays.toString(point.getArgs())+";目标方法的返回值为"+result);

}

return result;

}

}

5.2. 在spring配置文件中注入需要增强的对象和增强对象

 

 

 

 

 

 

如果想学习Java性能优化,工程化、高性能及分布式、深入浅出。微服务、Spring,MyBatis,Netty源码分析的朋友可以加下454377428群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给大家。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326177280&siteId=291194637