[Spring] AOP(02) - POJO
in Tech-Stack on Spring
AOP - POJO
์ด ํฌ์คํธ์์ [Spring] AOP(01) ๋ฅผ POJO ๋ฐฉ์์ผ๋ก ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ๊ฒ์ํ๋ค.
POJO
- Play Old Java Object
- ์ด๋ ํ ๊ฐ์ฒด์๋ ์์กดํ์ง ์๋ ์์ํ ์๋ฐ ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ค.
MyAspect ํด๋์ค๋ฅผ pojo ๊ฐ์ฒด๋ก ์์ ํ๋ค.
public class MyAspect {
public void bootingAndLogin(JoinPoint jp) {
String pwd = (String) jp.getArgs()[0];
System.out.println("์ปดํจํฐ ๋ถํ
์์. " + pwd + " ์
๋ ฅ ๋ก๊ทธ์ธ");
}
}
after๋ก ์คํํ pojo ๊ฐ์ฒด๋ฅผ ํ๋ ๋ ๋ง๋ ๋ค.
public class FinishAspect {
public void shutdown(JoinPoint jp) {
String com = jp.getTarget().getClass().getSimpleName();
System.out.println(com + " ์ปดํจํฐ ์ข
๋ฃ");
}
}
xml์ ํตํด ํด๋น ๊ฐ์ฒด๋ฅผ aspect๋ก ์ง์ ํ๋ค.
<bean id="myAspect" class="aop03.MyAspect" />
<bean id="finishAspect" class="aop03.FinishAspect" />
<bean id="programmer" class="aop03.Programmer" />
<aop:config>
<aop:aspect ref="myAspect">
<aop:before method="bootingAndLogin"
pointcut="execution(* usingComputer(..))" />
</aop:aspect>
<aop:aspect ref="finishAspect">
<aop:after method="shutdown"
pointcut="execution(* usingComputer(..))" />
</aop:aspect>
</aop:config>
<aop:aspect>- ref๋ก ์ค์ ๋๋ ๊ฐ์ฒด๋ฅผ aspect๋ก ์ง์ ํ๋ค.<aop:before>- target ๋ฉ์๋๊ฐ ์คํ๋๊ธฐ ์ ์ ์คํํ๋ค.method- aspect์์ target ๋ฉ์๋์คํ ์ /ํ์ ์คํํ ๋ฉ์๋๋ฅผ ์ค์ ํ๋ค.pointcut- target ๋ฉ์๋๋ฅผ ์ง์ ํ๋ค.
์ฝ๋๋ฅผ ์คํํ์ฌ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํด๋ณธ๋ค.
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("config.xml");
Person p1 = ctx.getBean("programmer", Person.class);
int useT1 = p1.usingComputer("1234");
System.out.println(p1.getClass().getName() + "์ด์ฉ์๊ฐ:" + useT1);
}
}
- ์คํ ๊ฒฐ๊ณผ
์ปดํจํฐ ๋ถํ ์์. 1234 ์ ๋ ฅ ๋ก๊ทธ์ธ [์ฝ๋ฉ์ ํ๋ค.] Programmer ์ปดํจํฐ ์ข ๋ฃ com.sun.proxy.$Proxy4์ด์ฉ์๊ฐ:200
