Category: Spring

0

Spring - Embedded Kafka 사용하기

목자 Spring - Kafka Spring - Kafka Java Config 사용하기 Spring - Embedded Kafka 사용하기 스프링 부트 카프카 사용하기참고https://www.baeldung.com/spring-boot-kafka-testing 의존성testImplementation 'org.springframework.boot:spring-boot-starter-test'testImplementation 'org.springframework.kafka:spring-kafka-test'// https://mvnrepository.com/artifact/org.testcontainers/kafkatestImplementation group: 'org.testcontainers', name: 'kafka', version: '1.16.3' Spring Propertiesspring: kafka: consumer: auto-offset-reset: earliest group-id: baeldungtest: topic: embedded-test-topic

0

Spring - Kafka Java Config 사용하기

목자 Spring - Kafka Spring - Kafka Java Config 사용하기 Spring - Embedded Kafka 사용하기 스프링 부트 카프카 사용하기참고https://www.baeldung.com/spring-kafka 목자 Post not found: spring-boot/spring-kafka/springboot-kafka Post not found: spring-boot/spring-kafka/springboot-kafka-javaconfig 의존성 추가implementation 'org.springframework.kafka:spring-kafka'testImplementation 'org.springframework.kafka:spring-kafka-test'

0

Spring Web Socket - Chat 프로그램 만들기 1

Spring boot Web Socket - Chat 프로그램 만들기 1참고 https://docs.spring.io/spring-framework/docs/4.3.x/spring-framework-reference/html/websocket.html https://daddyprogrammer.org/post/4731/spring-websocket-chatting-server-redis-pub-sub/ Message BrokerMessage Broker 는 Publisher 로부터 전달 받은 Message 를 Subscriber 로 전달하는 중간 역할을 하며 응용 소프트웨어 간에 메시지를 교환할 수 있게 한다. 이때, 메시지가 적재되는 공간은 Message Queue 라고 하며 메시지 그룹을 Topic 이라고 한다. Message Broker 는 데이터를 보내고 처리하고 삭제한다. Message Interceptor 정의하기 ChannelInterceptor 는 Message 를 MessageChannel 로 보내기 전과 Message 가 MessageChannel 로 보낸 후 추가적일 로직을 수행할 수 있게 도와주는 Interface 다. ChannelInterceptor 인터페이스를 구현해 Socket 통신 상태를 로그로 찍어 확인한다.

0

Spring - Kafka

스프링 부트 카프카 사용하기목자 Spring - Kafka Spring - Kafka Java Config 사용하기 Spring - Embedded Kafka 사용하기 의존성 추가implementation 'org.springframework.kafka:spring-kafka'testImplementation 'org.springframework.kafka:spring-kafka-test' 카프카에 접속하기 위한 설정Consumer 설정 spring.kafka.consumer.bootstrap-servers 카프카에 연결을 하기 위한 접속 주소 spring.kafka.consumer.group-id kafka Consumer 그룹에 대한 정보 spring.kafka.consumer.auto-offset-reset topic에 붙은 consumer의 offset 정보가 존재하지 않거나 오류가 발생해 offset을 사용할 수 없는 경우 처리하기 위한 옵션 latest : 가장 마지막 offset부터 earliest : 가장 처음 offset부터 none : offset 정보가 없으면 Exception 발생 spring.kafka.consumer.key-deserializer 카프카에서 전달받은 key 를 역질렬화 할때 사용하는 역질렬화 클래스 StringDeserializer 는 문자열 데이터만 사용 가능한 역직렬화 도구 spring.kafka.consumer.value-deserializer 카프카에서 전달받은 value 를 역질렬화 할때 사용하는 역질렬화 클래스 StringDeserializer 는 문자열 데이터만 사용 가능한 역직렬화 도구 Producer 설정

0

[Spring] - Embeded Redis 사용하기

목차 [Spring] - M1 맥북에서 Embeded Redis 사용하기 [Spring] - Embeded Redis 사용하기 참고https://www.baeldung.com/spring-embedded-redis ✅ Embeded Redis 사용하기 보통 Local 에서 테스트를 위해 많이 사용하며, 별도의 Redis 를 설치하지 않고 실행할 수 있게 해줍니다. 보통 Redis를 사용하는 스프링 프로젝트를 Local에서 프로젝트 실행시 Redis를 선행적으로 설치해줘야 합니다. 이 문제를 Embedded Redis를 사용해 프로젝트가 Local 환경에 의존적이지 않고 프로젝트 만으로 구동할 수 있도록 합니다. 1. Embedded Redis 사용을 위한 의존성 추가build.gradle 에 다음 Embedded Redis 사용을 위한 의존성을 추가해줍니다.

0

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

목차 [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] 포인트컷 포인트컷 표현식 - this, targetthis 와 target 은 객체를 대상으로 AOP 를 적용할 때 사용하는 포인트컷 표현식입니다. 두 표현식 모두 상위 타입이나 인터페이스를 이용해 AOP 를 적용할 수 있습니다. this 는 스프링 빈으로 등록돼 있는 프록시 객체를 대상으로 Advice 를 적용합니다. 때문에 스프링에서 프록시 객체를 생성하는 전략에 따라 AOP 가 다르게 적용될 수 있습니다. target 은 실제 객체를 대상으로 Advice 를 적용합니다. this 와 target 작동방식 확인을 위한 테스트 코드@AutowiredMemberService memberService;@Testvoid success() { log.info("memberService Proxy={}", memberService.getClass()); memberService.hello("helloA");}@Aspectstatic class ThisTargetAspect { //부모 타입 허용 @Around("this(hello.aop.member.MemberService)") public Object doThisInterface(ProceedingJoinPoint joinPoint) throws Throwable { log.info("[this-interface] {}", joinPoint.getSignature()); return joinPoint.proceed(); } //부모 타입 허용 @Around("target(hello.aop.member.MemberService)") public Object doTargetInterface(ProceedingJoinPoint joinPoint) throws Throwable { log.info("[target-interface] {}", joinPoint.getSignature()); return joinPoint.proceed(); } @Around("this(hello.aop.member.MemberServiceImpl)") public Object doThis(ProceedingJoinPoint joinPoint) throws Throwable { log.info("[this-impl] {}", joinPoint.getSignature()); return joinPoint.proceed(); } @Around("target(hello.aop.member.MemberServiceImpl)") public Object doTarget(ProceedingJoinPoint joinPoint) throws Throwable { log.info("[target-impl] {}", joinPoint.getSignature()); return joinPoint.proceed(); }} JDK 동적 프록시를 이용한 프록시 객체 생성

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