- AOP(Aspect-Oriented Programming) ์ ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ผ๋ถ ๊ณตํต์ ์ธ ๊ธฐ๋ฅ์ ๋ถ๋ฆฌํ์ฌ ์ฌ์ฌ์ฉํ๊ธฐ ์ํ ํ๋ก๊ทธ๋๋ฐ ํจ๋ฌ๋ค์์ด๋ค.
- ์ด๋ค ๋ก์ง์ ๊ธฐ์ค์ผ๋ก ํต์ฌ์ ์ธ ๊ด์ , ๋ถ๊ฐ์ ์ธ ๊ด์ ์ผ๋ก ๋๋์ด์ ๋ณด๊ณ ํด๋น ๊ด์ ์ ๊ธฐ์ค์ผ๋ก ๋ชจ๋ํํ๋ค.
- ํฉ์ด์ง ๊ด์ฌ์ฌ๋ฅผ ๋ชจ๋ํํ ์ ์๋ ํ๋ก๊ทธ๋๋ฐ ๊ธฐ๋ฒ
- ์ ๊ทธ๋ฆผ์์ ํด๋์ค A, B, C์ ๊ณตํต์ผ๋ก ๋ํ๋๋ ์์์ ๋ธ๋ก์ ์ค๋ณต๋๋ ๋ฉ์๋, ํ๋ ๋ฑ์ด๋ค.
- ์๋ก ํด๋์ค A์ ๋
ธ๋์ ๋ธ๋ก ์ฝ๋๋ฅผ ์์ ํด์ผ ํ๋ค๋ฉด, ๋ค๋ฅธ ํด๋์ค๋ค์์ ํด๋น ๋ธ๋ก์ ์ฐพ์์ ์ผ์ผ์ด ์์ ํด์ผ ํ๋ ์ํฉ์ด ๋ฐ์ํ๋ค.
- SOLID ์์น์ ์๋ฐฐํจ
- ๊ทธ๋ฆผ์ ์๋์ฒ๋ผ ๊ฐ์ ์์์ ๋ธ๋ก์ ๋ชจ๋ํ ์์ผ๋๊ณ ์ด๋์ ์ ์ฉ์ํฌ์ง๋ง ์ ์ํด์ฃผ๋ฉด ๋๋ค.
- AOP๋ ์ฃผ๋ก ๋ฉ์๋ ํธ์ถ ์ /ํ, ์์ธ ๋ฐ์ ์ ๋ฑ ๊ณตํต์ ์ธ ๋ก์ง(Aspect)์ ๋ถ๋ฆฌํ์ฌ ๊ด๋ฆฌํ๋ค.
- ์ด ๋ Aspect๋ ์๋์ ๊ฐ์ด ๊ตฌ์ฑ๋๋ค.
- Advice
- ์ค์ ์ํํ ๊ณตํต ๋ก์ง์ ์ ์ํ๋ค.
- Before, After, Around ๋ฑ์ด ์กด์ฌํ๋ค.
- Join point
- Advice๊ฐ ์คํ๋ ์ ์๋ ์ง์
- ๋ฉ์๋ ํธ์ถ ์ /ํ, ์์ธ ๋ฐ์ ์ ๋ฑ์ด Join point ์ด๋ค.
- Pointcut
- Join point์ ์งํฉ์ผ๋ก, Advice๊ฐ ์ ์ฉ๋ Join point๋ฅผ ์ ๋ณํ๋ค.
- Weaving
- Aspect๋ฅผ ์ ์ฉํ๋ ๊ณผ์ ์ด๋ค.
- Aspect๊ฐ ์ ์ฉ๋์ด ๋์ํ ์ ์๋๋ก ์ฝ๋์ ์ฝ์ ํ๋ ๊ฒ์ ์๋ฏธํ๋ค.
- ๋ก๊น
- ํธ๋์ญ์ ๊ด๋ฆฌ
- ๋ณด์
- ์ด๋
ธํ
์ด์
๊ธฐ๋ฐ ์ค์
@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 { }
- 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"); } }
- XML ์ค์ ํ์ผ์