[Spring] AOP(02) - POJO


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
    





ยฉ 2022. by Yejin Ha

Powered by JihyunRyu