μ λ² ν¬μ€ν (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κ° λμνλ λ°©μ
λ§μ½ λΉμ μ΄ μλ§μ ν¨ν€μ§λ€κ³Ό ν΄λμ€λ€μ ν¬ν¨ν μμ€ν μ κ°κ³ μκ³ 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κ° νλ κ²μ λͺ¨λ νΈλμμ μ½λλ₯Ό κ°μ Έκ° νΈλμμ κ΄μ μ λͺ¨μΌκ³ λμ, λͺ¨λ νΈλ μ΄μ± μ½λμ κ°μ Έκ° ν κ΄μ μΌλ‘ λͺ¨μΌκ³ , μ΅μ’ μ μΌλ‘ λͺ¨λ μμΈμ²λ¦¬ λν ν κ΄μ μ λμμ§λ€.
κ·Έ νμ λΉμ¦λμ€ λ‘μ§κ³Ό λͺ¨λ μΆκ°μ μΈ κ΄μ λ€ μ¬μ΄μ λΆλͺ ν λΆλ¦¬κ° μκΈ΄λ€.
λνμ μΈ ν‘λ¨κ΄μ¬μ μκ° λ°λ‘ νΈλμμ , νΈλ μ΄μ±, μμΈμ²λ¦¬μ΄λ€. μ΄λ€μ λͺ¨λ ν΄λμ€μμ ꡬνλμ΄μΌ νμ§λ§, κ·Έ ν΄λμ€μ ν΅μ¬μ μΈ κΈ°λ₯μ μλ κ²λ€μ΄λ€. μ΄λ¬ν ν‘λ¨κ΄μ¬μ ν κ΄μ μΌλ‘ λ³΄κ³ νλμ λͺ¨λλ‘ κ΅¬ννμ¬ λΉμ¦λμ€λ‘μ§, μ¦ μ€μ κΈ°λ₯ꡬνμ½λμ ꡬλΆνλ€.
λκΈ