Spring 의 AOP (주석 기반)
5458 단어 [프레임]Spring
public class MathCalculator {
public int div(int i,int j){
return i/j;
}
}
절단면 류
@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+"}");
}
}
설정 클래스
@EnableAspectJAutoProxy
@Configuration
public class MainConfigOfAOP {
//
@Bean
public MathCalculator calculator(){
return new MathCalculator();
}
//
@Bean
public LogAspects logAspects(){
return new LogAspects();
}
}
AOP: [동적 에이전트]
프로그램 이 실행 되 는 동안 특정한 코드 를 지정 한 방법 이 지정 한 위치 에 동적 으로 삽입 하여 실행 하 는 프로 그래 밍 방식 을 말한다.
:
(@Before):logStart: (div)
(@After):logEnd: (div) ( )
(@AfterReturning):logReturn: (div)
(@AfterThrowing):logException: (div)
(@Around): , (joinPoint.procced())
AOP 원리: [용기 에 어떤 구성 요 소 를 등 록 했 는 지 보 세 요. 이 구성 요 소 는 언제 작 동 합 니까? 이 구성 요소 의 기능 은 무엇 입 니까?]
1. @ Enable AspectJAuto Proxy 는 무엇 입 니까?
@Import(AspectJAutoProxyRegistrar.class): AspectJAutoProxyRegistrar
AspectJAutoProxyRegistrar bean:BeanDefinetion
internalAutoProxyCreator=AnnotationAwareAspectJAutoProxyCreator
용기 에 AnnotationAware Aspect JAuto Proxy Creator 를 등록 합 니 다.
2、 AnnotationAwareAspectJAutoProxyCreator:
AnnotationAwareAspectJAutoProxyCreator
->AspectJAwareAdvisorAutoProxyCreator
->AbstractAdvisorAutoProxyCreator
->AbstractAutoProxyCreator
implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware
( bean )、 BeanFactory
흐름:
1)、 , ioc
2)、 , refresh()
3)、registerBeanPostProcessors(beanFactory); bean bean
1)、 ioc BeanPostProcessor
2)、 BeanPostProcessor
3)、 PriorityOrdered BeanPostProcessor
4)、 Ordered BeanPostProcessor
5)、 BeanPostProcessor
6)、 BeanPostProcessor, BeanPostProcessor ,
internalAutoProxyCreator 를 만 든 BeanPostProcessor [AnnotationAware AspectJAutoProxyCreator]
1)、 Bean
2)、populateBean: bean
3)、initializeBean: bean;
1)、invokeAwareMethods(): Aware
2)、applyBeanPostProcessorsBeforeInitialization(): postProcessBeforeInitialization()
3)、invokeInitMethods():
4)、applyBeanPostProcessorsAfterInitialization(): postProcessAfterInitialization();
4)、BeanPostProcessor(AnnotationAwareAspectJAutoProxyCreator) ----》aspectJAdvisorsBuilder
7)、 BeanPostProcessor BeanFactory :
beanFactory.addBeanPostProcessor(postProcessor);
요약:
1)、@EnableAspectJAutoProxy AOP
2)、@EnableAspectJAutoProxy AnnotationAwareAspectJAutoProxyCreator
3)、AnnotationAwareAspectJAutoProxyCreator ;
4)、 :
1)、registerBeanPostProcessors() , AnnotationAwareAspectJAutoProxyCreator
2)、finishBeanFactoryInitialization() bean
1)、
2)、AnnotationAwareAspectJAutoProxyCreator
3)、 ,
: , (Advisor); (cglib);
5)、 :
1)、
2)、CglibAopProxy.intercept();
1)、 ( MethodInterceptor)
2)、 , ;
3)、 :
: -》 -》 -》
: -》 -》 -》