코스 봄 주석 중심의 연구 노트 (팔) AOP

AOP

, 동작 프로그램의 지정된 위치 지정 방법으로 절단하는 코드의 일부를 프로그램을 실행하는 동안 동적으로 참조
1 AOP 모듈 도입 상기 AOP 스프링 (스프링 측면)
2 것은 비즈니스 로직 클래스 (MathCalculator)를 정의; 사업 인쇄 로그 논리 연산 (있어서 전은, 상기 방법이 종료 실행 비정상있어서, XXX 때)
.도 3을 참조하면, 로그 슬라이스 타입 (LogAspects) 정의의 방법은 동적 센싱 다음 여기서 MathCalculator.div를 행하는 요구 클래스 부;

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


4는 방법은 대상 클래스 레이블 실행 언제 (알림 주석) 잘라
5, 선박에 추가 된 컷 타입과 비즈니스 로직 클래스 (대상 방법은 클래스)
클래스는 클래스가 봄 섹션을 알려야합니다, (6) (애스펙트 클래스에 주석을 추가하기 : @Aspect)
[. 7] 구성 클래스 오픈 주석 계 @EnableAspectJAutoProxy [모드] 추가 AOP
       많은 @EnableXXX을 봄

 

AOP 개발

비즈니스 로직 클래스 추가

public class MathCalculator {
	
	public int div(int i,int j){
		System.out.println("MathCalculator...div...");
		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();
	}
}

테스트 카테고리

@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();
}

운영 결과

 

개요

AOP는 세 단계를 달성하기 위해
1) 및 로직 컴포넌트가 용기 클래스에 추가 비즈니스 자르고 스프링에게 잘라 클래스 (@Aspect)는
절단면에 기초하여 각각의 주석 알림 통지 방법에 표시된 2), 우면 봄 여기서 실행 (pointcut 표현)
3) 개방 AOP 기반의 주석 모드; @EnableAspectJAutoProxy

그는 188 개 원래의 논문을 발표 · 원의 찬양 (20) · 보기 20000 +

추천

출처blog.csdn.net/qq_36154832/article/details/104237695