Skip to content

Latest commit

ย 

History

History
120 lines (109 loc) ยท 5.22 KB

AOP.md

File metadata and controls

120 lines (109 loc) ยท 5.22 KB

AOP

AOP ๊ฐœ๋…

  • AOP(Aspect-Oriented Programming) ์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ผ๋ถ€ ๊ณตํ†ต์ ์ธ ๊ธฐ๋Šฅ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŒจ๋Ÿฌ๋‹ค์ž„์ด๋‹ค.
  • ์–ด๋–ค ๋กœ์ง์„ ๊ธฐ์ค€์œผ๋กœ ํ•ต์‹ฌ์ ์ธ ๊ด€์ , ๋ถ€๊ฐ€์ ์ธ ๊ด€์ ์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ๋ณด๊ณ  ํ•ด๋‹น ๊ด€์ ์„ ๊ธฐ์ค€์œผ๋กœ ๋ชจ๋“ˆํ™”ํ•œ๋‹ค.
    • ํฉ์–ด์ง„ ๊ด€์‹ฌ์‚ฌ๋ฅผ ๋ชจ๋“ˆํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ๋ฒ•

aop-example

  • ์œ„ ๊ทธ๋ฆผ์—์„œ ํด๋ž˜์Šค A, B, C์— ๊ณตํ†ต์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ์ƒ‰์ƒ์˜ ๋ธ”๋ก์€ ์ค‘๋ณต๋˜๋Š” ๋ฉ”์„œ๋“œ, ํ•„๋“œ ๋“ฑ์ด๋‹ค.
  • ์˜ˆ๋กœ ํด๋ž˜์Šค A์˜ ๋…ธ๋ž€์ƒ‰ ๋ธ”๋ก ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ๋‹ค๋ฅธ ํด๋ž˜์Šค๋“ค์—์„œ ํ•ด๋‹น ๋ธ”๋ก์„ ์ฐพ์•„์„œ ์ผ์ผ์ด ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•œ๋‹ค.
    • SOLID ์›์น™์„ ์œ„๋ฐฐํ•จ
  • ๊ทธ๋ฆผ์˜ ์•„๋ž˜์ฒ˜๋Ÿผ ๊ฐ™์€ ์ƒ‰์ƒ์˜ ๋ธ”๋ก์„ ๋ชจ๋“ˆํ™” ์‹œ์ผœ๋†“๊ณ  ์–ด๋””์— ์ ์šฉ์‹œํ‚ฌ์ง€๋งŒ ์ •์˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

AOP ๊ตฌ์„ฑ

  • AOP๋Š” ์ฃผ๋กœ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ „/ํ›„, ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ๋“ฑ ๊ณตํ†ต์ ์ธ ๋กœ์ง(Aspect)์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ด€๋ฆฌํ•œ๋‹ค.
  • ์ด ๋•Œ Aspect๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋œ๋‹ค.
  1. Advice
    • ์‹ค์ œ ์ˆ˜ํ–‰ํ•  ๊ณตํ†ต ๋กœ์ง์„ ์ •์˜ํ•œ๋‹ค.
    • Before, After, Around ๋“ฑ์ด ์กด์žฌํ•œ๋‹ค.
  2. Join point
    • Advice๊ฐ€ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋Š” ์ง€์ 
    • ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ „/ํ›„, ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ๋“ฑ์ด Join point ์ด๋‹ค.
  3. Pointcut
    • Join point์˜ ์ง‘ํ•ฉ์œผ๋กœ, Advice๊ฐ€ ์ ์šฉ๋  Join point๋ฅผ ์„ ๋ณ„ํ•œ๋‹ค.
  4. Weaving
    • Aspect๋ฅผ ์ ์šฉํ•˜๋Š” ๊ณผ์ •์ด๋‹ค.
    • Aspect๊ฐ€ ์ ์šฉ๋˜์–ด ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฝ”๋“œ์— ์‚ฝ์ž…ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

AOP ์ฃผ ์‚ฌ์šฉ์ฒ˜

  1. ๋กœ๊น…
  2. ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ
  3. ๋ณด์•ˆ

AOP ์ ์šฉ ๋ฐฉ๋ฒ•

  1. ์–ด๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜ ์„ค์ •
    • @Aspect ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ Aspect ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๊ณ , @Pointcut ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ Pointcut์„ ์ •์˜ํ•œ๋‹ค.
    • Advice ๋ฉ”์„œ๋“œ์—๋Š” @Before, @After, @Around ๋“ฑ์˜ Advice ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”์„œ๋“œ ์‹คํ–‰ ์ „/ํ›„, ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ๋“ฑ์— ์‹คํ–‰๋  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
    • ์ดํ›„ @Pointcut ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ Advice๊ฐ€ ์‹คํ–‰๋  Join point๋ฅผ ์ง€์ •ํ•œ๋‹ค.
    • // LoggingAspect.java
      @Aspect
      @Component
      public class LoggingAspect {
        @Before("execution(* com.example.service.*.*(..))")
        public void beforeAdvice() {
            System.out.println("๋ฉ”์„œ๋“œ ์‹คํ–‰ ์ „ ๋กœ๊น… ์ˆ˜ํ–‰");
        }
      }
      
      // UserService.java
      @Service
      public class UserService {
        public void createUser(String name) {
            System.out.println("์‚ฌ์šฉ์ž ์ƒ์„ฑ: " + name);
        }
      }
      
      // MainApp.java
      public class MainApp {
        public static void main(String[] args) {
            ApplicationContext context = new ApplicationConfigApplicationContext(AppConfig.class);
            UserService userService = context.getBean(UserService.class);
            userService.createUser("John");
        }
      }
      
      // AppConfig.java
      @Configuration
      @EnableAspectJAutoProxy
      @ComponentScan(basePackages = "com.example")
      public class AppConfig {
      }
  2. XML ๊ธฐ๋ฐ˜ ์„ค์ •
    • XML ์„ค์ • ํŒŒ์ผ์— <aop:config> ์š”์†Œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Aspect์™€ Pointcut, Advice๋ฅผ ์ •์˜ํ•œ๋‹ค.
    • <aop:config> ์š”์†Œ ๋‚ด์—์„œ <aop:aspect> ์š”์†Œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Aspect๋ฅผ ์ •์˜ํ•˜๊ณ , <aop:pointcut>๊ณผ <aop:advice> ์š”์†Œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Pointcut๊ณผ Advice๋ฅผ ์ •์˜ํ•œ๋‹ค.
    • <!-- applicationContext.xml -->
      <beans xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
      
        <!-- Aspect ์ •์˜ -->
        <aop:config>
           <aop:aspect id="loggingAspect" ref="loggingAspectBean">
               <!-- Pointcut ์ •์˜ -->
               <aop:pointcut id="executionPointcut" expression="execution(* com.example.service.*.*(..))" />
               <!-- Advice ์ •์˜ -->
               <aop:before pointcut-ref="executionPointcut" method="beforeAdvice" />
           </aop:aspect>
        </aop:config>
      
        <!-- Aspect Bean ์ •์˜ -->
        <bean id="loggingAspectBean" class="com.example.aspect.LoggingAspect" />
      
        <!-- ์„œ๋น„์Šค Bean ์ •์˜ -->
        <bean id="userService" class="com.example.service.UserService" />
      </beans>
    • // LoggingAspect.java
      public class LoggingAspect {
         public void beforeAdvice() {
             System.out.println("๋ฉ”์„œ๋“œ ์‹คํ–‰ ์ „ ๋กœ๊น… ์ˆ˜ํ–‰");
         }
      }
      
      // UserService.java
      public class UserService {
         public void createUser(String name) {
             System.out.println("์‚ฌ์šฉ์ž ์ƒ์„ฑ: " + name);
         }
      }
      
      // MainApp.java
      public class MainApp {
         public static void main(String[] args) {
             ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
             UserService userService = (UserService) context.getBean("userService");
             userService.createUser("John");
         }
      }