ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring Boot] AOP(Aspect Oriented Programming) ์ ์šฉํ•˜๊ธฐ
    Spring Boot 2022. 10. 24. 22:31

    ๐ŸŒˆ AOP(Aspect Oriented Programming)๋ž€?

    ์–ด๋–ค ๋กœ์ง์„ ๊ธฐ์ค€์œผ๋กœ ํ•ต์‹ฌ์ ์ธ ๊ด€์ , ๋ถ€๊ฐ€์ ์ธ ๊ด€์ ์œผ๋กœ ๋‚˜๋ˆ ๋ณด๊ณ  ๊ทธ ๊ด€์ ์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ๊ฐ ๋ชจ๋“ˆํ™” ํ•œ๋‹ค.

    • ํ•ต์‹ฌ์ ์ธ ๊ด€์ : ๊ฐœ๋ฐœ์ž๊ฐ€ ์ ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ํ•ต์‹ฌ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง.
    • ๋ถ€๊ฐ€์ ์ธ ๊ด€์ : ํ•ต์‹ฌ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ DB ์—ฐ๊ฒฐ(JDBC), ํŒŒ์ผ ์ž…์ถœ๋ ฅ, ๋กœ๊น…

    build.gradle

    implementation 'org.springframework.boot:spring-boot-starter-aop'

     

    Enable AOP

    @EnableAspectJAutoProxy
    Appplication ํด๋ž˜์Šค์— ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

    @EnableAspectJAutoProxy
    @SpringBootApplication
    public class AopApplication {
        public static void main(String[] args) {
            SpringApplication.run(AopApplication.class, args);
        }
    }

     

    @Around("execution(์ ์šฉํ•  ๊ทœ์น™ ์ž‘์„ฑ)")

    1. ์ ‘๊ทผ ์ œํ•œ์ž ํŒจํ„ด
    โ†ช public [์ƒ๋žต ๊ฐ€๋Šฅ]

    2. ๋ฆฌํ„ด ํƒ€์ž… ํŒจํ„ด
    โ†ช long

    3. ํŒจํ‚ค์ง€๋ช…, ํด๋ž˜์Šค ๊ฒฝ๋กœ ํŒจํ„ด
    โ†ช com.admin.service.TestService [์ƒ๋žต ๊ฐ€๋Šฅ]

    4. ๋ฉ”์„œ๋“œ๋ช… ํŒจํ„ด(ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž… ํŒจํ„ด 1|ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž… ํŒจํ„ด 2)
    โ†ช save(String, String)

    5. throws ์˜ˆ์™ธ ํƒ€์ž… ํŒจํ„ด
    โ†ช throws RuntimeException [์ƒ๋žต ๊ฐ€๋Šฅ]


    *๋Š” ๋ชจ๋“  ๊ฐ’์„ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค.

    @Around("execution(* com..service.*Service.save*(..))") ์™€ ๊ฐ™์ด ์ž‘์„ฑ ํ•  ๊ฒฝ์šฐ
    โ†ช com.*.service ํŒจํ‚ค์ง€์˜ *Service๋กœ ๋๋‚˜๋Š” ํด๋ž˜์Šค์•ˆ์— save๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฉ”์„œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ตฌ๋ถ„ ์—†์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

    @Slf4j
    @Aspect
    @Component
    public class LogAspect {
        @Around("execution(* com..service.*Service.save*(..))")
        public Object saveLogging(ProceedingJoinPoint pjp) throws Throwable {
            Object result = pjp.proceed();
            log.info("==> LogAspect saveLogging Root : {}", pjp.getSignature().getDeclaringTypeName());
            log.info("==> LogAspect saveLogging Method : {}", pjp.getSignature().getName());
            return result;
        }
    }

     

    ํ…Œ์ŠคํŠธ
    [io-20020-exec-6] com.admin.aspect.LogAspect      : ==> LogAspect saveLogging Root : com.admin.service.TestService
    [io-20020-exec-6] com.admin.aspect.LogAspect      : ==> LogAspect saveLogging Method : saveTest

     

    ์—๋Ÿฌ

    ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ๋กœ์˜ ํด๋ž˜์Šค, ๋นˆ, ์ž˜๋ชป๋œ ํŒจํ„ด์„ ์ ์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.
    ์ด๋Ÿด ๊ฒฝ์šฐ @Around์— ์ž‘์„ฑํ•œ ๋‚ด์šฉ์„ ๋‹ค์‹œ ํ™•์ธํ•ด๋ณธ๋‹ค.

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: warning no match for this type name: com.service.service [Xlint:invalidAbsoluteTypeName]

    ๋Œ“๊ธ€

Designed by Tistory.