๋ณด์กฐ ์ ๋ฌด์ ์ฌ์ฉ์ ๋ฐ๋ผ ์คํ๋ง์ Before Advice, After returnning Advice, After throwing Advice, Around Advice ๋ค ๊ฐ์ง๋ฅผ ์ ๊ณตํ๋ค. ์ด์ ํฌ์คํ ์์ ์์ ์๋ฐ๋ก๋ง AOP๋ฅผ ๊ตฌํํ๋ ์ฝ๋๋ฅผ ์์ ํ์ฌ Around Advice๋ฅผ ๊ตฌํํด ๋ณด๋๋ก ํ๊ฒ ๋ค. (unounou.tistory.com/48)
Program.java
public class Program {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring/aop/setting.xml");
Exam proxy = (Exam) context.getBean("proxy");
System.out.printf("total is %d\n", proxy.total());
System.out.printf("avg is %d\n", proxy.total());
}
}
LogAroundAdvice ํด๋์ค๋ฅผ ์์ฑํ์ฌ proxy๋ฅผ ๋ฐ๋ก ๋นผ๋๋๋ค. invoke ๋ฉ์๋๊ฐ proxy์ ๊ฐ๋ค๊ณ ์๊ฐํ๋ฉด ๋๋ค.
LogAroundAdvice.java
public class LogAroundAdvice implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
/////////๋ณด์กฐ ์
๋ฌด/////////
long start = System.currentTimeMillis();
/////////๋ณด์กฐ ์
๋ฌด/////////
//////////์ฃผ ์
๋ฌด//////////
Object result = invocation.proceed(); //proceed : invoke์ ๊ฐ์ ์ญํ
//////////์ฃผ ์
๋ฌด//////////
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
/////////๋ณด์กฐ ์
๋ฌด/////////
long end = System.currentTimeMillis();
String message = (end - start) + "์๊ฐ์ด ๊ฑธ๋ ธ์ต๋๋ค.";
System.out.println(message);
/////////๋ณด์กฐ ์
๋ฌด/////////
return result;
}
}
์คํ๋ง ์ค์ ์ ๋นผ๋๊ธฐ ์ํด setting.xml ํ์ผ์ ์ถ๊ฐํ๋ค.
setting.xml
<bean id="target" class="spring.aop.entity.NewlecExam" p:kor="1" p:eng="1" p:math="1" p:com="1"/>
<bean id="logAroundAdvice" class="spring.aop.advice.LogAroundAdvice"/>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="target"/> <!-- setTarget()์ ์๋ฏธ -->
<property name="interceptorNames">
<list>
<value>logAroundAdvice</value> <!-- ์ฐธ์กฐํ๋ ๊ฐ์ฒด์ ์ด๋ฆ -->
</list>
</property>
</bean>
๋ด๋ ์ฒ ์คํ๋ง ํ๋ ์์ํฌ ๊ฐ์๋ฅผ ๋ฃ๊ณ ์ ๋ฆฌํ ๊ฒ์๊ธ์ ๋๋ค.
'๐ปStudy > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring MVC ๊ฐ๋ฐํ๊ฒฝ ์ธํ / Dispatcher Servlet / View Resolver (0) | 2021.02.20 |
---|---|
Spring MVC (0) | 2021.02.14 |
์์ ์๋ฐ๋ก AOP ๊ตฌํํด๋ณด๊ธฐ (0) | 2021.02.13 |
AOP(Aspect Oriented Programming)์ ๊ฐ๋ (0) | 2021.02.13 |
XML Configuration์ Java Configuration์ผ๋ก ๋ณ๊ฒฝํ๊ธฐ (0) | 2021.02.13 |