Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 피보나치함수 예제
- 재귀함수 예제
- katalon
- 피보나치 예제
- CSTS 폭포수 모델
- 한국투자증권 양도세 신고
- tomcat log
- 한국투자증권 해외주식 양도세
- 재귀 예제
- Katalon Recorder 사용법
- 테스트 자동화
- 최대공약수 예제
- 국세청 해외주식 양도세 신고방식
- katalon 자동화
- 피보나치함수
- bfs 미로탐색 java
- 해외증권 양도세 한국투자증권
- java.sql.SQLSyntaxErrorException
- katalon xpath
- git 연동
- 주식 양도세 신고방법
- js 자동완성
- recursion example
- oracle group by
- 홈택스 해외주식 양도세
- 해외주식 양도세 신고
- katalon 비교
- katalon 사용법
- 톰캣 실시간 로그
- javascript 자동완성
Archives
- Today
- Total
엄지월드
Spring AOP request 로그 저장 본문
[배경]
사용자들이 홈페이지에 들어와서 어느 메뉴를 많이 사용하는지 알고 싶다는 니즈가 있었다.
하지만 모든 controller마다 로그를 넣기에는 번거롭기 때문에 공통 관심사를 AOP로 분리하였다.
[소스]
Controller 부분에서만 @Before을 통해 메소드가 실행되기 전에 실행되도록 처리하였다.
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogAspect {
@Before("execution (* com.project.web..*.*Controller.*(..))")
public void before() throws Throwable {
HttpServletRequest request =
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
System.out.println(request.getRequestURI());
// TODO 로그 DB에 저장 로직 추가
}
}
[주의점]
주의점은 *Controller.*을 하지 않고 빌드를 하게 되면 아래와 같이 에러가 난다.
왜냐하면 모든 요청에서 requestMappingHandlerMapping이 존재하는 것은 아니기 때문이다.
그래서 Controller에서만 적용시켜주어야 한다.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping]: Factory method 'requestMappingHandlerMapping' threw exception; nested exception is java.lang.StackOverflowError
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at com.dodrambio.web.WebApplication.main(WebApplication.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping]: Factory method 'requestMappingHandlerMapping' threw exception; nested exception is java.lang.StackOverflowError
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651)
... 24 common frames omitted
'java > Spring' 카테고리의 다른 글
gradle을 활용한 war 배포(boot + eclipse) (0) | 2023.01.18 |
---|---|
@Mapper 어노테이션 (0) | 2023.01.17 |
Intellij target xml 미생성 오류 해결 (0) | 2022.12.31 |
eclipse spring intellij 실행 (0) | 2022.12.31 |
Error injecting constructor, java.lang.ExceptionInInitializerError: Cannot access defaults field of Properties (0) | 2022.12.30 |
Comments