Home

0

[Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 Advice 에 매게변수 전달포인트 컷 포현식을 사용해서 AOP 가 적용되는 객체나 메소들의 정보를 Advice 내 매개변수로 정보들을 전달 할 수 있습니다. Advice 로 매개변수로 정보들을 넘기기 위해서는 포인트컷의 이름과 매개변수 이름이 같아야 합니다. 또한, 값이 들어올때 타입이 매개변수에서 정의한 타입으로 제한이 됩니다. AOP 적용 확인을 위한 테스트 코드@AutowiredMemberService memberService;@Testvoid success() { log.info("memberService Proxy={}", memberService.getClass()); memberService.hello("helloA");} 메소드 매개변수값 정보를 Advice 에 전달args 를 이용하면 메소드에 전달된 매개변수의 값 정보를 가져올 수 있습니다.

0

[Spring AOP] 포인트컷 표현식 - bean

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 beanbean 표현식은 스프링에서만 사용한 포인트컷 지시자로 Bean 이름으로 Advice 적용 여부를 판단합니다. @Slf4j@Import(BeanTest.BeanAspect.class)@SpringBootTestpublic class BeanTest { @Autowired OrderService orderService; @Test void success(){ orderService.orderItem("itemA"); } @Aspect static class BeanAspect{ // orderService 빈과 Repository 로 끝나는 빈들에 Advice 를 적용합니다. @Around("bean(orderService) || bean(*Repository)") public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable { log.info("[bean] {}", joinPoint.getSignature()); return joinPoint.proceed(); } }} 2021-12-21 00:13:43.463 INFO 35836 --- [ Test worker] com.example.aop.pointcut.BeanTest : [bean] void com.example.aop.order.OrderService.orderItem(String)2021-12-21 00:13:43.474 INFO 35836 --- [ Test worker] com.example.aop.order.OrderService : [orderService] 실행2021-12-21 00:13:43.475 INFO 35836 --- [ Test worker] com.example.aop.pointcut.BeanTest : [bean] String com.example.aop.order.OrderRepository.save(String)2021-12-21 00:13:43.480 INFO 35836 --- [ Test worker] com.example.aop.order.OrderRepository : [orderRepository] 실행

0

[Spring AOP] 포인트컷 표현식 - @annotation

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 @annotation 메서드가 주어진 어노테이션을 가지고 있는 조인 포인트를 매칭 @annotation 은 특정 어노테이션이 적용된 메소드 를 기준으로 Advice 를 적용합니다. Custom Annotation 생성@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface MethodAop { String value();} Advisor 생성어노테이션을 이용해 Advice 적용하기 위해 @annotation 에 어노테이션 패키지 경로를 지정합니다.

0

[Spring AOP] 포인트컷 표현식 - @args

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 @args 메서드내 매개변수에 특정 어노테이션을 가지고 있는 경우 조인 포인트를 매칭 @args 는 메서드내 매개변수가 특정 어노테이션을 가지고 있는 경우 Advice 를 적용합니다. 런타임 시점에서 해당 어노테이션이 실제로 존재하는지 확인합니다. 커스텀 어노테이션@Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation {} Advisor 생성매개변수가 @MyAnnotation 를 가질 경우 advice 를 적용하기 위해 Adivsor 를 생성합니다.

0

[Spring AOP] 포인트컷 표현식 - @target, @within

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 @target 과 @within 클래스에 특정 어노테이션이 적용된 경우 조인 포인트를 매칭 @target 과 @within 은 어노테이션을 기준으로 AOP를 적용하기 위해 사용되는 포인트컷 표현식입니다. 특정 어노테이션이 적용된 클래스내 모든 메서드에 AOP 를 적용합니다. @target 은 런타임 객체의 실제 타입 을 기준으로 매칭합니다. 이 과정에서 해당 객체의 부모 클래스나 인터페이스에 선언된 어노테이션도 포함하여 검사하기 때문에 부모 타입의 메서드까지 Advice 를 다 적용합니다. @within 경우 컴파일 시점 에 특정 클래스 에 어노테이션이 선언되었는지를 기준으로 확인하기 때문에 정의된 클래스내 메서드에만 Advice 를 적용합니다. Custon Annotation 생성

0

[Spring AOP] 포인트컷 표현식 - args

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 포인트컷 표현식 - args args 는 메서드의 매개변수를 기준으로 AOP 를 적용 args는 메서드의 매개변수를 기준으로 매칭하기 위한 표현식입니다. 메서드의 매개변수 타입이나 값을 기준으로 특정 메서드에만 AOP를 적용할 수 있습니다. 테스트를 위한 객체@ClassAop@Componentpublic class MemberServiceImpl implements MemberService { @Override @MethodAop("test value") public String hello(String param) { return "ok"; } private String internal(String param) { return "ok"; }} args 를 이용해 적용 여부 판단@BeforeEachpublic void init() throws NoSuchMethodException { helloMethod = MemberServiceImpl.class.getMethod("hello", String.class);}private AspectJExpressionPointcut pointcut(String expression) { AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(expression); return pointcut;}@Testvoid args() { // hello(String)과 매칭 assertThat(pointcut("args(String)") .matches(helloMethod, MemberServiceImpl.class)).isTrue(); // Object 는 String 의 상위 타입 assertThat(pointcut("args(Object)") .matches(helloMethod, MemberServiceImpl.class)).isTrue(); assertThat(pointcut("args()") .matches(helloMethod, MemberServiceImpl.class)).isFalse(); assertThat(pointcut("args(..)") .matches(helloMethod, MemberServiceImpl.class)).isTrue(); assertThat(pointcut("args(*)") .matches(helloMethod, MemberServiceImpl.class)).isTrue(); assertThat(pointcut("args(String,..)") .matches(helloMethod, MemberServiceImpl.class)).isTrue();}

0

[Spring AOP] 포인트컷 표현식 - within

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 within within 은 특정 타입 에 대해 Advice 를 적용합니다. 특정 타입이 within 을 만족 하면 해당 타입내 모든 메소드는 Advice 가 적용됩니다. // com.example.aop.member.MemberServiceImpl 타입을 대상으로 Advice 를 적용합니다.within(com.example.aop.member.MemberServiceImpl)// com.example.aop.member 패키지내 타입 이름에 Service 가 들어가면 Advice 를 적욯합니다.within(com.example.aop.member.*Service*)// com.example.aop 패키지와 하위 패키지내 모든 타입에 Advice 를 적용합니다.within(com.example.aop..*) execution 과 within 의 차이within 은 표현식은 execution 과 다르게 부모 타입을 지정했을 경우 자식 타입에는 Advice 가 적용되지 않습니다. 즉, 상속이나 구현을 통해 생성된 객체에는 Advice 가 적용되지 않고 정확하게 지정된 타입에만 적용되는 점에서 execution 과 차이가 있습니다. @Test@DisplayName("타겟의 타입에만 직접 적용, 인터페이스를 선정하면 안된다.")void withinSuperTypeFalse() { pointcut.setExpression("within(com.example.aop.member.MemberService)"); assertThat(pointcut.matches(helloMethod, MemberServiceImpl.class)).isFalse();}@Test@DisplayName("execution은 타입 기반, 인터페이스 선정 가능")void executionSuperTypeTrue() { pointcut.setExpression("execution(* com.example.aop.member.MemberService.*(..))"); assertThat(pointcut.matches(helloMethod, MemberServiceImpl.class)).isTrue();}

0

[Spring AOP] 포인트컷 표현식 - execution

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 포인트컷 표현식 - execution execution( [접근제어자] 리턴타입 [선언타입] 메소드이름(파라미터) [예외] ) execution 는 포인트컷 표현식에서 가장 많이 사용되는 명시자 입니다. execution 는 메소드의 접근 제어자, 리턴 타입, 메소드가 선언된 패키지, 클래스 정보, 메소드 파라미터, 예외처리 정보를 이용해 다양한 조건으로 포인트컷 을 적용할 수 있도록 제공합니다. 접근제어자, 선언타입, 예외의 경우는 생각이 가능합니다. execution 사용 예시execution(public String com.example.aop.member.MemberServiceImpl.hello(String))

0

[Spring AOP] 포인트컷

목차 [Spring AOP] 포인트컷 표현식 - this, target [Spring AOP] 포인트컷 표현식 - Advice 에 매게변수 전달 [Spring AOP] 포인트컷 표현식 - bean [Spring AOP] 포인트컷 표현식 - @args [Spring AOP] 포인트컷 표현식 - @annotation [Spring AOP] 포인트컷 표현식 - @target, @within [Spring AOP] 포인트컷 표현식 - args [Spring AOP] 포인트컷 표현식 - within [Spring AOP] 포인트컷 표현식 - execution [Spring AOP] 포인트컷 @Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)public @interface ClassAop {} @Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface MethodAop { String value();} public interface MemberService { String hello(String param);} @ClassAop@Componentpublic class MemberServiceImpl implements MemberService { @Override @MethodAop("test") public String hello(String param) { return null; } public String internal(String param){ return "ok"; }} AspectJExpressionPointcut 이 포인트 컷 표현식을 처리해주는 클래스 @Slf4jpublic class ExecutionTest { AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); Method helloMethod; @BeforeEach public void init() throws NoSuchMethodException { helloMethod = MemberServiceImpl.class.getMethod("hello", String.class); } @Test void printMethod(){ log.info("helloMethod = {}", helloMethod); }}

0

Java - Collection Framework

Java - Collection FrameworkList 컬랙션 List 컬랙션은 객체를 일렬로 늘여 놓은 구조 를 가지고 있다. 객체를 인덱스로 관리하기 때문에 객체를 저장하면 인덱스가 부여되고 해당 인덱스를 이용해 검색, 삭제를 할 수 있다. List 컬랙션에서는 객체 자체를 저장하는 게 아니라 객체의 번지를 참조한다. 동일한 객체를 중복 저장할 수 있다. 대표적인 구현체로는 ArrayList, Vector, LinkedList 등이 있다. 메서드 설명 boolean add(E e) List 맨 끝에 데이터를 추가한 후 정상적으로 저장되면 true 실패하면 false 를 반환한다. void add(int index, E element) 주어진 인덱스 위치에 데이터를 추가한다. set(int index, E element) 주어진 인덱스 위치에 저장된 데이터를 변경한다. boolean contains(Object o) 주어진 데이터가 List에 있는지 확인한다. E get(int index) 주어진 인덱스에 저장된 데이터를 반환한다. isEmpty() List 에 데이터가 있는지 확인한다. int size() List 에저장 돼 있는 데이터의 전체 크기를 반환한다. E remove(int index) 주어진 인덱스에 위치한 데이터를 삭제한다. void clear() List 에 저장돼 있는 모든 원소들을 삭제한다. boolean remove(Object o) List 에서 전달받은 데이터에 해당 하는 모든 원소를 삭제한다. Set 컬랙션 저장 순서가 유지되지 않기 때문에 Index 가 존재하지 않는다. 대신, Iterator 를 이용해 Set 에 저장된 데이터를 가져올 수 있다. 객체를 중복해서 저장할 수 없고, 하나의 null 만 저장할 수 있다. 대표적인 구현체로는 HashSet, TreeSet 이 있다. 메서드 설명 boolean add(E e) 데이터를 Set 에 저장한 후 정상적으로 저장되면 true를 중복 객체면 false 를 반환합니다. boolean contains(Object o) 객체가 저장돼 있는지 여부를 리턴합니다. Iterator iterator() 저장된 객체를 한번씩 가져오는 반복자를 반환한다. isEmpty() Set 이 비어있는지 조사합니다. int Size() Set 에 저장돼 있는 전체 객체수를 리턴합니다. void clear() Set 에 저장된 모든 객체를 삭제 boolean remove(Object o) Set 에 저장된 주어진 객체를 삭제합니다. Map 컬랙션 Key, Value 형식 으로 데이터를 관리하는 자료구조 키는 중복 저장될 수 없지만 값은 중복 저장될 수 있다. 기존에 저장된 키와 동일한 키로 값을 저장하면 새로운 값으로 대체된다. 대표적인 구현체로는 HashMap, HashTable, LinkedHashMap, TreeMap 등이 있다. 메서드 설명 V put(K Key, V value) Map 에 주어진 키와 값을 추가해 저장되면 해당 값을 리턴합니다. boolean containsKey(Object Key) Map 에 주어진 키 가 있는지 확인합니다. boolean containsValue(Object value) Map 에 주어진 값 이 있는지 확인합니다. Set<Map.Entry<K,V>> entrySet() 모든 Map.Entry 객체를 Set에 담아 리턴합니다. Set keySet() 모든 키를 Set 객체에 담아서 리턴합니다. V get(Object key) 주어진 키에 해당하는 값을 리턴합니다. boolean isEmpty() Map 이 비어있는지 조사합니다. int Size() Map 에 저장돼 있는 전체 객체의 수를 리턴합니다. Collection values() Map 에 저장된 모든 값을 Collection 객체에 담아서 리턴합니다. void clear() 저장된 모든 Map.Entry를 삭제합니다. V remove(Object Key) 주어진 키와 일치하는 Map.Entry를 삭제하고 값을 리턴합니다.