Skip to content

Commit

Permalink
고급편/섹션11: 예제 만들기 (#21)
Browse files Browse the repository at this point in the history
섹션11: 스프링 AOP - 포인트컷
  • Loading branch information
toychip committed Mar 22, 2024
1 parent 7981986 commit d7b56bc
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package hello.aop.member;

public interface MemberService {
String hello(String param);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package hello.aop.member;

import hello.aop.member.annotation.ClassAop;
import hello.aop.member.annotation.MethodAop;
import org.springframework.stereotype.Component;

@ClassAop
@Component
public class MemberServiceImpl implements MemberService {
@Override
@MethodAop("test value")
public String hello(final String param) {
return "ok";
}

public String internal(String param) {
return "ok";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package hello.aop.member.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ClassAop {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hello.aop.member.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodAop {
String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package hello.aop.pointcut;

import hello.aop.member.MemberServiceImpl;
import java.lang.reflect.Method;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;

@Slf4j
public class ExecutionTest {

AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
Method helloMethod;

@BeforeEach
public void init() throws NoSuchMethodException {
// 테스트에서 실행할 수 있게 getMethod()를 추출해서 helloMethod에 보관
helloMethod = MemberServiceImpl.class.getMethod("hello", String.class);
}

@Test
void printMethod() {

// 앞으로 알아볼 execution으로 시작하는 포인트컷 표현식은 이 메서드 정보를 매칭해서 포인트컷 대상을 찾아낸다.
// execution(* ..package..Class.)
// public java.lang.String hello.aop.member.MemberServiceImpl.hello(java.lang.String)
log.info("helloMethod={}", helloMethod);
}
}

0 comments on commit d7b56bc

Please sign in to comment.