728x90
1. 종속성 추가
implementation 'org.springframework.boot:spring-boot-starter-aop'
2. main 클래스에 @EnableAspectJAutoProxy 추가
@EnableAspectJAutoProxy // AOP 활성화
public class FirstApplication {
public static void main(String[] args) {
SpringApplication.run(FirstApplication.class, args);
}
}
3. AOP Aspect 클래스 생성
package com.example.first.aop;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Aspect
@Component
@Slf4j
@RequiredArgsConstructor
public class LogAopComponent {
private Logger logger = LoggerFactory.getLogger(LogAopComponent.class);
// 적용하고 싶은 경로 적어주기
@AfterReturning(pointcut = "execution(* com.example.first..*(..))", returning = "result")
public void logMethodExecution(Object result) {
log.info("test");
}
}
경로는 모든 프로젝트의 디렉토리로 설정했다.
이렇게 하면,
프로젝트 내 모든 메서드가 실행될 때, test 로그가 찍히는 것을 볼 수 있다!!!
'Back-end > java' 카테고리의 다른 글
AOP를 사용하여 에러 로그 찍기 (0) | 2023.09.26 |
---|---|
로컬 디스크에 자바 로깅 기록하기 (spring.log) (0) | 2023.09.26 |
빌드 중 NoSuchFileException 에러 미해결 (0) | 2023.09.26 |
게시판 페이징 기능 완료 (0) | 2023.09.24 |
파일 업로드 기능 구현 중 에러 - IllegalStateException: File has been moved - cannot be read again (0) | 2023.09.21 |