스프링 AOP - 빠른 시작
다음은 AOP를 다룰 때 접하게 될 몇 가지 주요 용어입니다.
조언
조인포인트
포인트컷
구문: Advice("execution(modifiers? return-type declaring-type? method-name(param) throws?) ")
@Before("execution(public Integer com.company.service.*.add*(..))")_ 예시
0. 패키지 구성
전:
com.company
com.company.aop
com.company.service
1. Maven/pom.xml에 추가
<!-- aop dependency -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2. AOP 클래스 생성 및 주석 적용
com.company.aop.MyCustomAspect
@Aspect  // import from org.aspectj.lang.annotation.Aspect
@Component
public class MyCustomAspect{
   // code goes here..
}
3. 이러한 조언 방법을 AOP 클래스에 추가하십시오.
상담 전
@Before("execution(public Integer com.company.service.*.add*(..))") 
public void beforeLoggingAdd() { 
   System.out.println("### LOG: advice that runs BEFORE the method ###"); 
}
조언 후
@After("execution(public Integer com.company.service.*.add*(..))") 
public void afterLoggingAdd() { 
   System.out.println("### LOG: advice that runs AFTER the method ###"); 
}
조언 반환 후
선택적 반환 = "반환"은 일치하는 방법의 반환 값에 액세스하는 데 사용할 수 있습니다
@AfterReturning(pointcut = "execution(public Integer com.company.service.*.add*(..))", returning = "result") 
public void afterReturningLoggingAdd(Object result) { 
   System.out.println("### LOG: advice that runs AFTER the methods's successful execution ###"); 
   System.out.println("result: " + result.toString()); 
}
조언을 던진 후
@AfterThrowing(pointcut = "execution(public Integer com.company.service.*.add*(..))", throwing = "throwingException") 
public void afterExceptionLoggingAdd(Throwable throwingException) { 
   System.out.println("### LOG: advice that runs after the method (if exception is thrown) ###"); 
   System.out.println("exception: " + throwingException); 
}
어드바이스 주변
이 어드바이스는 메서드 호출 전후에 사용자 지정 동작을 수행할 수 있습니다.
@Around("execution(public Integer com.company.service.*.add*(..))") 
public Integer aroundLoggingAdd(ProceedingJoinPoint jp) throws Throwable { 
   System.out.println("### LOG: advice that runs BEFORE and AFTER the method ###"); 
   Integer result = (Integer) jp.proceed(); 
   System.out.println("### LOGGING ADD METHOD : AROUND ###"); 
   return result;
}
4. 실행할 메소드 생성
com.company.service.MyService
@Service
public class MyService {
   public Integer addResource(){
      System.out.println("add method executed!");
      return 0;
   }
}
5. AOP 어드바이스 테스트
com.company.DemoApplication
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
   @Autowired
   MyService ms;
   @Override
   public void run(String... args) throws Exception {
      ms.addResource();
   }
}
Reference
이 문제에 관하여(스프링 AOP - 빠른 시작), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/tommyc/spring-aop-quick-start-3o39텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)