λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Basics/Concepts

Spring AOP : 관점 지ν–₯ ν”„λ‘œκ·Έλž˜λ°

by IworldT 2020. 10. 25.
λ°˜μ‘ν˜•

μ €λ²ˆ ν¬μŠ€νŒ…(iworldt.tistory.com/13)에 이어, 이번 ν¬μŠ€νŒ… μ—­μ‹œ λ―Έλ””μ—„(meduim.com)μ—μ„œ λ³Έ 글을 λ²ˆμ—­ν•΄λ³΄λŠ” ν¬μŠ€νŒ…μ΄λ‹€. μ €λ²ˆ ν¬μŠ€νŒ…μ˜ μ£Όμ œλŠ” μŠ€ν”„λ§ ν”„λ ˆμž„μ›Œν¬μ˜ μ˜μ‘΄μ„± μ£Όμž…μ΄μ—ˆλ‹€.

이번 ν¬μŠ€νŒ…μ˜ μ£Όμ œλŠ” μŠ€ν”„λ§ ν”„λ ˆμž„μ›Œν¬μ˜ 관점지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ΄λ‹€. μ£Όμ œλŠ” What is Spring AOP? 이닀. 

(medium.com/@raffai.89.zoltan/what-is-spring-aop-f5191cc1b54c)이 원본이닀!

μš°λ¦¬κ°€ Aspect Oriented Programming, 즉 관점 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ— λŒ€ν•œ 정보λ₯Ό λ°œμ·Œν•  수 μžˆλŠ” λΆ€λΆ„μ˜ λ²ˆμ—­λ§Œ μž‘μ„±ν•˜λ„λ‘ ν•˜κ² λ‹€. λ¬Όλ‘  λ‚˜μ˜ 주관적인 해석이 λ“€μ–΄κ°€ μžˆμœΌλ―€λ‘œ, λ‚˜μ˜ 해석을 μ°Έκ³ ν•˜μ—¬ 단어듀을 κ²€μƒ‰ν•˜λ©° μ •ν™•ν•œ 정보λ₯Ό μ°Ύμ•„κ°ˆ 수 있기λ₯Ό λ°”λž€λ‹€. λ˜ν•œ, ν‹€λ¦° 뢀뢄이 μžˆλ‹€λ©΄ μ•Œλ €μ£Όμ‹ λ‹€λ©΄ κ°μ‚¬ν•˜κ² λ‹€!

What is Aspect Oriented Programming(AOP)?

Aspect-Oriented programming is a programming paradigm which tries to solve the problems of cross-cutting concerns. Aspect-oriented Programming (AOP) complements Object-oriented Programming (OOP) by providing another way of thinking about program structure.

The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect.

관점지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ΄ 무엇인가?

관점 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ€ νš‘λ‹¨κ΄€μ‹¬(cross-cutting concerns) λ¬Έμ œλ₯Ό ν•΄κ²°ν•˜λ„λ‘ ν•˜λŠ” ν”„λ‘œκ·Έλž˜λ° νŒ¨λŸ¬λ‹€μž„μ΄λ‹€. 관점지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ€ 객체지ν–₯ν”„λ‘œκ·Έλž˜λ°(Object-oriented Programming)을 ν”„λ‘œκ·Έλž¨ ꡬ쑰에 λŒ€ν•΄ λ˜λ‹€λ₯Έ 관점을 μ œκ³΅ν•¨μœΌλ‘œμ¨ λ³΄μ™„ν•œλ‹€.

객체지ν–₯ν”„λ‘œκ·Έλž˜λ°μ˜ λͺ¨λ“ˆν™”μ˜ 핡심 μœ λ‹›μ€ 클래슀인 λ°˜λ©΄μ— 관점지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ˜ λͺ¨λ“ˆν™”μ˜ μœ λ‹›μ€ 관점이닀.

μš°μ„ , cross-cutting concerns(νš‘λ‹¨κ΄€μ‹¬)에 λŒ€ν•΄ μ•Œμ•„λ³΄μž. μ΄κ²ƒμ˜ λ°˜λŒ€λŠ” core concerns(핡심 관심)이닀. ν•΅μ‹¬κ΄€μ‹¬λž€, 객체지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ—μ„œ ν•˜λ‚˜μ˜ 클래슀둜 ν‘œν˜„λ˜κ³  μžˆλŠ” 관심사이닀. ν•˜λ‚˜μ˜ κΈ°λŠ₯이라고 μ΄ν•΄ν•˜λ©΄ νŽΈν•  것이닀. κ·ΈλŸ¬λ‚˜ νš‘λ‹¨κ΄€μ‹¬μ€, μ—¬λŸ¬ 핡심관심 ν΄λž˜μŠ€λ“€μ— 걸쳐 κ΅¬ν˜„λ˜κ³ μžˆλŠ” κ΄€μ‹¬μœΌλ‘œ ν΄λž˜μŠ€λ“€μ„ κ°€λ‘œμ§€λ₯΄κ³  μžˆμ–΄ νš‘λ‹¨ 관심이라고 ν‘œν˜„ν•œλ‹€. 이 νš‘λ‹¨κ΄€μ‹¬μ€ μ—¬λŸ¬ ν΄λž˜μŠ€μ—μ„œ ν•„μˆ˜μ μ΄κ³ , κ·Έλž˜μ„œ λ°˜λ³΅λ˜λŠ” μ½”λ“œμ΄μ§€λ§Œ ν•΅μ‹¬μ½”λ“œλŠ” μ•„λ‹Œ 관심이닀. μ΄κ²ƒμ˜ μ˜ˆλŠ” λ’€μ—μ„œ μ‚΄νŽ΄λ³Έλ‹€.

In a more simpler word, it helps us to refactor the different necessary repeating codes into different modules. Which gives us the benefit that we can maintain these functionalities in one single place, instead of it writing down every time.

더 κ°„λ‹¨νžˆ μ„€λͺ…ν•˜λ©΄, 이것은 μ„œλ‘œλ‹€λ₯Έ ν•„μˆ˜μ μΈ 반볡 μ½”λ“œλ₯Ό λ‹€λ₯Έ λͺ¨λ“ˆλ‘œ μ½”λ“œμ˜ ꡬ쑰λ₯Ό μž¬μ‘°μ •ν•˜λ„λ‘ λ•λŠ”λ‹€. μš°λ¦¬μ—κ²Œ μ£ΌλŠ” 이점은 맀번 계속 μ¨λ‚΄λ €κ°€λŠ” λŒ€μ‹  ν•˜λ‚˜μ˜ λ…λ¦½λœ κ³΅κ°„μ—μ„œ κΈ°λŠ₯듀을 μœ μ§€λ³΄μˆ˜ν•  수 μžˆλ‹€λŠ” 것이닀.

μ—¬κΈ°μ„œ λ§ν•˜λŠ” μ„œλ‘œλ‹€λ₯Έ ν•„μˆ˜μ μΈ 반볡 μ½”λ“œκ°€ λ°”λ‘œ νš‘λ‹¨κ΄€μ‹¬μ΄λ‹€. μ—¬λŸ¬ ν΄λž˜μŠ€μ— 걸쳐 κ΅¬ν˜„λœ νš‘λ‹¨κ΄€μ‹¬μ„ ν•˜λ‚˜μ˜ λͺ¨λ“ˆλ‘œ κ΅¬ν˜„ν•˜μ—¬ ꡬ쑰λ₯Ό μž¬μ‘°μ •ν•œλ‹€λŠ” 것이닀.

This approach will result in a much more maintainable code, which clears the business logic from the most confusion factor. We separate these functionalities along different aspects.

An aspect is a modularization of a concern that cuts across multiple classes. Unified logging or transaction management can be a good example of it.

이 μ ‘κ·Ό(관점지ν–₯)은 λ”λ§Žμ€ μœ μ§€λ³΄μˆ˜κ°€λŠ₯ν•œ μ½”λ“œλ₯Ό λ§Œλ“€μ–΄λ‚΄λŠ”λ°, λŒ€λΆ€λΆ„μ˜ ν˜Όλž€μš”μΈμœΌλ‘œλΆ€ν„° λΉ„μ¦ˆλ‹ˆμŠ€ λ‘œμ§μ„ κΉ¨λ—ν•˜κ²Œ ν•œλ‹€. μš°λ¦¬λŠ” μ΄λŸ¬ν•œ κΈ°λŠ₯듀을 μ„œλ‘œλ‹€λ₯Έ κ΄€μ λ“€λ‘œ λΆ„λ¦¬ν•œλ‹€.

ν•œ 관점은 μ—¬λŸ¬ ν΄λž˜μŠ€μ— 걸친 ν•œ κ΄€μ‹¬μ‚¬μ˜ λͺ¨λ“ˆν™”이닀. 톡합 λ‘œκΉ… λ˜λŠ” νŠΈλžœμž­μ…˜ 관리가 쒋은 μ˜ˆμ‹œμ΄λ‹€.

μœ μ§€λ³΄μˆ˜κ°€λŠ₯ν•˜λ‹€λŠ” 것은, νš‘λ‹¨κ΄€μ‹¬μ„ ν•˜λ‚˜μ˜ λͺ¨λ“ˆλ‘œ κ΄€λ¦¬ν•¨μœΌλ‘œμ¨ μ„œλ‘œμ—κ²Œ λΌμΉ˜λŠ” 영ν–₯을 μ€„μž„μœΌλ‘œμ¨ μ½”λ“œμ˜ 지속적인 관리λ₯Ό μ‰½κ²Œ ν•œλ‹€λŠ” 뜻으둜 λ³Ό 수 μžˆλ‹€.  μ—¬λŸ¬ ν΄λž˜μŠ€μ— 걸친 ν•œ κ΄€μ‹¬μ‚¬μ˜ λͺ¨λ“ˆν™” , 즉 ν•˜λ‚˜μ˜ νš‘λ‹¨κ΄€μ‹¬μ„ ν•œ 관점 으둜 λ³΄λŠ” 것이 λ°”λ‘œ 관점지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ΄λ‹€.

How AOP works on a large scale

If you have a system that contains several packages and classes and you do not use AOP that aspects such as Tracing, Transactions, and Exception Handling, have to implement in every class and every method. This results in two problems:

큰 λ²”μœ„μ—μ„œ AOPκ°€ λ™μž‘ν•˜λŠ” 방식

cross-concern νš‘λ‹¨κ΄€μ‹¬

λ§Œμ•½ 당신이 μˆ˜λ§Žμ€ νŒ¨ν‚€μ§€λ“€κ³Ό ν΄λž˜μŠ€λ“€μ„ ν¬ν•¨ν•œ μ‹œμŠ€ν…œμ„ κ°–κ³ μžˆκ³  Tracing, Transactions, Exception Handlingκ³Ό 같은 κ΄€μ μ˜ AOPλ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€λ©΄, λͺ¨λ“  ν΄λž˜μŠ€μ™€ λͺ¨λ“  λ©”μ†Œλ“œλ§ˆλ‹€ κ΅¬ν˜„ν•΄μ•Όν•œλ‹€. 이것은 두 κ°€μ§€μ˜ 문제λ₯Ό λ§Œλ“ λ‹€.

  • Code Tangling: Each class, each method contains Tracing, Transactions, and Exception Handling, and also Business Logic. In a tangled code it is often hard to see what is actually going on in a method.
  • Code Scattering: Aspects such as Transactions are scattered throughout the code and not implemented in a single specific part of the system.

 

  • Code Tangling(μ½”λ“œ μ—‰ν‚΄) : 각 ν΄λž˜μŠ€μ™€ 각 λ©”μ†Œλ“œλŠ” Transactions, Exception Handlingκ³Ό λΉ„μ¦ˆλ‹ˆμŠ€ 둜직 λ˜ν•œ ν¬ν•¨ν•˜κ³  μžˆλ‹€. μ΄λ ‡κ²Œ 엉킨 μ½”λ“œλŠ” λ©”μ†Œλ“œμ—μ„œ μ‹€μ§ˆμ μœΌλ‘œ μ–΄λ–€ 일이 μˆ˜ν–‰λ˜λŠ”μ§€ μ•ŒκΈ° νž˜λ“€λ‹€.

ν•˜λ‚˜μ˜ λ©”μ†Œλ“œμ—λŠ” ν•˜λ‚˜μ˜ κΈ°λŠ₯, ν•˜λ‚˜μ˜ λ³€κ²½μ΄μœ λ§Œ μ‘΄μž¬ν•΄μ•Ό ν•œλ‹€. νš‘λ‹¨κ΄€μ‹¬κ³Ό 핡심관심이 κ³΅μ‘΄ν•˜λ©΄ 이 ν”„λ‘œκ·Έλž˜λ° 원칙을 μ–΄κΈ°κ²Œ λœλ‹€.

  • Code scattering(μ½”λ“œ μ‚°λž€) : νŠΈλžœμž­μ…˜κ³Ό 같은 관점듀은 λͺ¨λ“  μ½”λ“œμ— 걸쳐 λΆ„μ‚°λ˜κ³  μ‹œμŠ€ν…œμ˜ νŠΉμ •ν•œ λ…λ¦½λœ 뢀뢄에 κ΅¬ν˜„λ˜μ§€ μ•ŠλŠ”λ‹€.

Using AOP, allows you to solve these problems. So what AOP does is it takes all the Transaction code, and puts it into a Transaction Aspect, then it takes all the Tracing Code and puts that into an Aspect and finally, all the Exception Handling is also put into an Aspect.

Afterward, there will be a clean separation between the Business Logic and all those additional aspects.

AOPλŠ” 이런 λ¬Έμ œλ“€μ„ ν•΄κ²°ν•΄μ€€λ‹€. AOPκ°€ ν•˜λŠ” 것은 λͺ¨λ“  νŠΈλžœμž­μ…˜ μ½”λ“œλ₯Ό κ°€μ Έκ°€ νŠΈλžœμž­μ…˜ 관점에 λͺ¨μœΌκ³ λ‚˜μ„œ, λͺ¨λ“  νŠΈλ ˆμ΄μ‹± μ½”λ“œμ„ κ°€μ Έκ°€ ν•œ κ΄€μ μœΌλ‘œ λͺ¨μœΌκ³ , μ΅œμ’…μ μœΌλ‘œ λͺ¨λ“  μ˜ˆμ™Έμ²˜λ¦¬ λ˜ν•œ ν•œ 관점에 놓아진닀.

κ·Έ 후에 λΉ„μ¦ˆλ‹ˆμŠ€ 둜직과 λͺ¨λ“  좔가적인 관점듀 사이에 λΆ„λͺ…ν•œ 뢄리가 생긴닀.

λŒ€ν‘œμ μΈ νš‘λ‹¨κ΄€μ‹¬μ˜ μ˜ˆκ°€ λ°”λ‘œ νŠΈλžœμž­μ…˜, νŠΈλ ˆμ΄μ‹±, μ˜ˆμ™Έμ²˜λ¦¬μ΄λ‹€. 이듀은 λͺ¨λ“  ν΄λž˜μŠ€μ—μ„œ κ΅¬ν˜„λ˜μ–΄μ•Ό ν•˜μ§€λ§Œ, κ·Έ 클래슀의 핡심적인 κΈ°λŠ₯은 μ•„λ‹Œ 것듀이닀. μ΄λŸ¬ν•œ νš‘λ‹¨κ΄€μ‹¬μ„ ν•œ κ΄€μ μœΌλ‘œ 보고 ν•˜λ‚˜μ˜ λͺ¨λ“ˆλ‘œ κ΅¬ν˜„ν•˜μ—¬ λΉ„μ¦ˆλ‹ˆμŠ€λ‘œμ§, 즉 μ‹€μ œ κΈ°λŠ₯κ΅¬ν˜„μ½”λ“œμ™€ κ΅¬λΆ„ν•œλ‹€.  

728x90
λ°˜μ‘ν˜•

λŒ“κΈ€