AOP _코드

JoMinJun·2021년 4월 14일

SPRING

목록 보기
2/2

Target
개발자가 작성한 service클래스
Joint Point
Target의 매서드 service의 클래스 매서드

package com.spring.myapp.aop.service;

import org.springframework.stereotype.Service;

@Service
public class HelloService implements IHelloService {

	@Override
	public String sayHello(String name) {
		// TODO Auto-generated method stub
		String message = "Hello" + name;
		System.out.println("HelloService.sayHello()실행");
		return message;
	}

	@Override
	public String sayGoodBye(String name) {
		// TODO Auto-generated method stub
		String message = "bye" + name;
		System.out.println("HelloService.sayGoodBye()실행");
		return message;
	}

}

PointCut

package com.spring.myapp;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LogAspect {


//1번 방법
	//*(private , public ..) 패키지, 클래스, 매서드명(파라미터명)
	@Pointcut(value = "execution(* com.spring..*.sayHello(..))")
	private void helloPointcut() {}
	
	@Before("helloPointcut()")
	public void BeforeLog(JoinPoint joinPoint) {
		Signature signature = joinPoint.getSignature();
		System.out.println(signature.getName());
	}
	
	
	
	

// 2번 방법	
   
	@After("execution(* com.spring..*.*(..))")
	public void AfterLog(JoinPoint joinPoint) {
		Signature signature = joinPoint.getSignature();
		System.out.println(signature.getName());
	}
}
profile
기술정리

0개의 댓글