Course Spring annotation-driven study notes (eight) AOP

AOP

Refers to dynamic during the program run a section of code will cut into the specified location designation method of programming operation;
1, introducing aop module; the AOP the Spring: (Spring-Aspects)
2, define a business logic class (MathCalculator); business when the log logic operation for printing (before the method, the method ends run, abnormal method, XXX)
. 3, the log defines a slice type (LogAspects): class section which methods require the dynamic sensing operation is then performed where MathCalculator.div;

通知方法:
前置通知(@Before):logStart:在目标方法(div)运行之前运行
后置通知(@After):logEnd:在目标方法(div)运行结束之后运行(无论方法正常结束还是异常结束)
返回通知(@AfterReturning):logReturn:在目标方法(div)正常返回之后运行
异常通知(@AfterThrowing):logException:在目标方法(div)出现异常以后运行
环绕通知(@Around):动态代理,手动推进目标方法运行(joinPoint.procced())


4, a method to cut the target class label run anytime (notification annotations);
5, the cut type and business logic classes (class where the target method) are added to the vessel;
6, must tell Spring section which class is the class (to add a comment on the aspect class: @Aspect)
[. 7], added to the configuration class open annotation based @EnableAspectJAutoProxy [mode] aop
       many @EnableXXX in Spring

 

AOP development

Add business logic classes

public class MathCalculator {
	
	public int div(int i,int j){
		System.out.println("MathCalculator...div...");
		return i/j;	
	}

}

Add cut class

@Aspect
public class LogAspects {
	
	//抽取公共的切入点表达式
	//1、本类引用
	//2、其他的切面引用
	@Pointcut("execution(public int com.atguigu.aop.MathCalculator.*(..))")
	public void pointCut(){};
	
	//@Before在目标方法之前切入;切入点表达式(指定在哪个方法切入)
	@Before("pointCut()")
	public void logStart(JoinPoint joinPoint){
		Object[] args = joinPoint.getArgs();
		System.out.println(""+joinPoint.getSignature().getName()+"运行。。。@Before:参数列表是:{"+Arrays.asList(args)+"}");
	}
	
	@After("com.atguigu.aop.LogAspects.pointCut()")
	public void logEnd(JoinPoint joinPoint){
		System.out.println(""+joinPoint.getSignature().getName()+"结束。。。@After");
	}
	
	//JoinPoint一定要出现在参数表的第一位
	@AfterReturning(value="pointCut()",returning="result")
	public void logReturn(JoinPoint joinPoint,Object result){
		System.out.println(""+joinPoint.getSignature().getName()+"正常返回。。。@AfterReturning:运行结果:{"+result+"}");
	}
	
	@AfterThrowing(value="pointCut()",throwing="exception")
	public void logException(JoinPoint joinPoint,Exception exception){
		System.out.println(""+joinPoint.getSignature().getName()+"异常。。。异常信息:{"+exception+"}");
	}

}

Add configuration class

@EnableAspectJAutoProxy
@Configuration
public class MainConfigOfAOP {
	 
	//业务逻辑类加入容器中
	@Bean
	public MathCalculator calculator(){
		return new MathCalculator();
	}

	//切面类加入到容器中
	@Bean
	public LogAspects logAspects(){
		return new LogAspects();
	}
}

Test category

@Test
public void test01() {
	AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
			MainConfigOfAOP.class);

	//1、不要自己创建对象
	MathCalculator mathCalculator1 = new MathCalculator();
	mathCalculator1.div(1, 1);
	System.out.println("----------------------------");
	MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class);
        mathCalculator.div(1, 10);
	applicationContext.close();
}

operation result

 

to sum up

AOP to achieve three steps
1) and cut the business logic components are added to the vessel classes; tell Spring which is cut class (@Aspect)
2) marked on each annotation notification notification method based on the cut surface, to tell when Spring where run (pointcut expression)
3) open aop based annotation mode; @EnableAspectJAutoProxy

He published 188 original articles · won praise 20 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_36154832/article/details/104237695