인프런 커뮤니티 질문&답변

avenue님의 프로필 이미지

작성한 질문수

스프링 핵심 원리 - 고급편

AnnotationAwareAspectJAutoProxyCreator 적용 오류

22.08.22 15:56 작성

·

306

0

package InflearnSpringCoreAdvanced.CoreProxy.proxy.config.v5_autoproxy;

import InflearnSpringCoreAdvanced.CoreProxy.proxy.config.AppV1Config;
import InflearnSpringCoreAdvanced.CoreProxy.proxy.config.AppV2Config;
import InflearnSpringCoreAdvanced.CoreProxy.proxy.config.v3_proxyfactory.advice.LogTraceAdvice;
import InflearnSpringCoreAdvanced.CoreProxy.proxy.trace.logtrace.LogTrace;
import org.springframework.aop.Advisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({AppV1Config.class, AppV2Config.class})
public class AutoProxyConfig {

@Bean
public Advisor advisor1(LogTrace logTrace) {
// Pointcut
NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
pointcut.setMappedNames("request*", "order*", "save*");

// Advice
LogTraceAdvice advice = new LogTraceAdvice(logTrace);

// Advisor
return new DefaultPointcutAdvisor(pointcut, advice);
}
}
package InflearnSpringCoreAdvanced.CoreProxy;

import InflearnSpringCoreAdvanced.CoreProxy.proxy.config.v5_autoproxy.AutoProxyConfig;
import InflearnSpringCoreAdvanced.CoreProxy.proxy.trace.logtrace.LogTrace;
import InflearnSpringCoreAdvanced.CoreProxy.proxy.trace.logtrace.ThreadLocalLogTrace;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

//@Import({AppV1Config.class, AppV2Config.class})
//@Import(InterfaceProxyConfig.class)
//@Import(ConcreteProxyConfig.class)
//@Import(DynamicProxyBasicConfig.class)
//@Import(DynamicProxyFilterConfig.class)
//@Import(ProxyFactoryConfigV1.class)
//@Import(ProxyFactoryConfigV2.class)
//@Import(BeanPostProcessorConfig.class)
@Import(AutoProxyConfig.class)
@SpringBootApplication(scanBasePackages = "InflearnSpringCoreAdvanced.CoreProxy.proxy.app")
public class CoreProxyApplication {

public static void main(String[] args) {
SpringApplication.run(CoreProxyApplication.class, args);
}

@Bean
public LogTrace logTrace() {
return new ThreadLocalLogTrace();
}

}

 

현재 AutoProxyConfig, main의 코드는 위와 같고 이를 실행하면 다음과 같은 오류가 발생합니다

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderControllerV1' defined in InflearnSpringCoreAdvanced.CoreProxy.proxy.config.AppV1Config: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [InflearnSpringCoreAdvanced.CoreProxy.proxy.app.v1.OrderControllerV1]: Factory method 'orderControllerV1' threw exception; nested exception is java.lang.NullPointerException: Cannot invoke "InflearnSpringCoreAdvanced.CoreProxy.proxy.trace.TraceStatus.getTraceId()" because "status" is null

at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.22.jar:5.3.22]

at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486) ~[spring-beans-5.3.22.jar:5.3.22]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-5.3.22.jar:5.3.22]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) ~[spring-beans-5.3.22.jar:5.3.22]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.22.jar:5.3.22]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.22.jar:5.3.22]

at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.22.jar:5.3.22]

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.22.jar:5.3.22]

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.22.jar:5.3.22]

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.22.jar:5.3.22]

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.22.jar:5.3.22]

at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.22.jar:5.3.22]

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.22.jar:5.3.22]

at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.3.jar:2.7.3]

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.3.jar:2.7.3]

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.3.jar:2.7.3]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.3.jar:2.7.3]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.3.jar:2.7.3]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.3.jar:2.7.3]

at InflearnSpringCoreAdvanced.CoreProxy.CoreProxyApplication.main(CoreProxyApplication.java:24) ~[classes/:na]

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [InflearnSpringCoreAdvanced.CoreProxy.proxy.app.v1.OrderControllerV1]: Factory method 'orderControllerV1' threw exception; nested exception is java.lang.NullPointerException: Cannot invoke "InflearnSpringCoreAdvanced.CoreProxy.proxy.trace.TraceStatus.getTraceId()" because "status" is null

at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.22.jar:5.3.22]

at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.22.jar:5.3.22]

... 19 common frames omitted

Caused by: java.lang.NullPointerException: Cannot invoke "InflearnSpringCoreAdvanced.CoreProxy.proxy.trace.TraceStatus.getTraceId()" because "status" is null

at InflearnSpringCoreAdvanced.CoreProxy.proxy.trace.logtrace.ThreadLocalLogTrace.response(ThreadLocalLogTrace.java:45) ~[classes/:na]

at InflearnSpringCoreAdvanced.CoreProxy.proxy.trace.logtrace.ThreadLocalLogTrace.exception(ThreadLocalLogTrace.java:41) ~[classes/:na]

at InflearnSpringCoreAdvanced.CoreProxy.proxy.config.v3_proxyfactory.advice.LogTraceAdvice.invoke(LogTraceAdvice.java:33) ~[classes/:na]

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.22.jar:5.3.22]

at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) ~[spring-aop-5.3.22.jar:5.3.22]

at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708) ~[spring-aop-5.3.22.jar:5.3.22]

at InflearnSpringCoreAdvanced.CoreProxy.proxy.config.AppV1Config$$EnhancerBySpringCGLIB$$f7cc6e87.orderControllerV1(<generated>) ~[classes/:na]

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]

at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]

at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.22.jar:5.3.22]

... 20 common frames omitted

 

 

결국 ThreadLocalLogTrace에서 response에서의 TraceStatus가 null값이 들어가서 생긴 NPE 오류라고 나오는데 이전에 AnnotationAwareAspectJAutoProxyCreator가 아닌 직접 빈 후처리기를 만들고 등록할때까지는 아무런 오류가 없었는데 AnnotationAwareAspectJAutoProxyCreator를 등록하고 난 후에 main자체가 띄워지지가 않는데 어디가 문제인가요??

 

build.gradle에는 aop적용하였고 버전을 낮춰봤는데도 버전문제가 아닌거 같습니다

답변 3

1

김영한님의 프로필 이미지
김영한
지식공유자

2022. 08. 27. 18:35

안녕하세요. avenue님

전체 프로젝트를 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.

구글 드라이브 업로드 방법은 다음을 참고해주세요.

https://bit.ly/3fX6ygx

주의: 업로드시 링크에 있는 권한 문제 꼭 확인해주세요

추가로 다음 내용도 코멘트 부탁드립니다.

1. 실행 방법을 알려주세요.

2. 어떻게 문제를 확인할 수 있는지 자세한 설명을 남겨주세요.

감사합니다.

avenue님의 프로필 이미지
avenue
질문자

2022. 08. 28. 13:12

링크 : https://drive.google.com/file/d/1xHlkzLcT5ZOYP5X7FqIOjoIatewJq7cW/view?usp=sharing

현재 링크상에 압축된 파일은 서버를 기동하면 [Exception(logTrace.exception(statue, e) -> response(status, e) -> status.getTraceId()]에서 NPE 오류가 발생합니다

여기서 AutoProxyConfig상에 (AppV1Config, AppV2Config)에 대한 Import를 지우면 v3에 대해서만 정상적으로 작동하고 v1, v2도 작동을 하기 위해서는 직접 main에서 v1, v2에 대한 @Bean을 등록하면 정상적으로 작동이 됩니다

0

김영한님의 프로필 이미지
김영한
지식공유자

2022. 09. 04. 23:16

안녕하세요.

LogTraceAdvice 코드에 잘못된 부분이 있습니다.

LogTraceAdvice에 다음 코드에 보이는 추가 부분을 찍어보시면 바로 잘못된 부분을 확인하실 수 있을거에요.

} catch (Exception e) {
    log.error("error", e); //추가
    logTrace.exception(status, e);
    throw e;
}

감사합니다.

avenue님의 프로필 이미지
avenue
질문자

2022. 09. 06. 21:41

악,,, 바로 보이네요.... 다시 강의 열심히 들으러 가겠습니다..ㅎㅎ

0

avenue님의 프로필 이미지
avenue
질문자

2022. 08. 24. 18:34

AppV1Config, AppV2Config내부에 v1, v2 빈 자체를 main에다가 빈 생성 후 실행을 시키면 정상적으로 돌아가는데 @Import({AppV1Config.class, AppV2Config.class})로 따로 불러와서 빈으로 등록하려면 오류가 발생하는데 이 원인을 잘 모르겠습니다...

avenue님의 프로필 이미지

작성한 질문수

질문하기