[Spring] AOP(01)
in Tech-Stack on Spring
AOP(Aspect Oriented Programing)
๊ด์ ์งํฅ ํ๋ก๊ทธ๋๋ฐ
ํน์ ํ์๋ฅผ ํน์ ์์ ๋ง๋ค ์คํํ ๊ฒฝ์ฐ ์ฌ์ฉํ๋ค.
AOP๋ฅผ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ 2๊ฐ์ง๊ฐ ์๋ค.
- java ์ฝ๋๋ก ๊ตฌํ
- spring์์ ์ ๊ณตํ๋ aspect ์ฌ์ฉ
1. java ์ฝ๋๋ก ๊ตฌํ
์ด๋ค ๋ฉ์๋๊ฐ ์คํ๋๋ proxy ํด๋์ค๋ฅผ ๋ฌด์กฐ๊ฑด ์ง๋๊ฐ๋๋ก ์ค์ ํด์ ํน์ ํ์๋ฅผ ํ๋๋ก ์ค์
์ด๋ค ๊ฐ์ฒด์ด๋ usingComputer() ๋ฉ์๋๊ฐ ์คํ๋๊ธฐ ์ง์ ์ โ๋ถํ
ํ [๋น๋ฐ๋ฒํธ] ์
๋ ฅํ์ฌ ๋ก๊ทธ์ธโ ์ ์ถ๋ ฅํด๋ณด์.
- ํด๋น ๋ฉ์๋๋ฅผ ๊ฐ๊ณ ์๋ interface Person์ ์์ฑํ๋ค. ๋ชจ๋ ํด๋์ค๋ ํด๋น interface๋ฅผ ๊ตฌํํ๋ค.
public interface Person { public int usingComputer(String pwd); } - Person์ ๊ตฌํํ๋ program ํด๋์ค๋ฅผ ์์ฑํ๋ค.
public class Program implements Person{ @Override public int usingComputer(String pwd) { try { // ํต์ฌ ๊ธฐ๋ฅ System.out.println("[๊ฒ์์ ํ๋ค.]"); }catch(Exception e) { if(e.getMessage().equals("์ค๋ฅ!")) { System.out.println("AS๋ฅผ ์ ์ฒญํ๋ค."); } } System.out.println("์ปดํจํฐ ์ข ๋ฃ"); return 100; } } - Person์ ๊ตฌํํ๋ ๋ชจ๋ ํด๋์ค๊ฐ ์ง๋๊ฐ๋ Proxy ํด๋์ค ์ ์ธ
public class Proxy implements Person { private Person delegate; public Proxy(Person delegate) { this.delegate = delegate; } @Override public int usingComputer(String pwd) { System.out.println("๋ถํ ํ " + pwd + "์ ๋ ฅํ์ฌ ๋ก๊ทธ์ธ"); return delegate.usingComputer(pwd); } } - ์คํํ์ฌ ๊ฒฐ๊ณผ๋ฅผ ํ์ธ
public class Main { public static void main(String[] args) { Person p1 = new Proxy(new Program()); int useT1 = p1.usingComputer("1234"); System.out.println(p1.getClass().getName() + "์ด์ฉ์๊ฐ:" + useT1); } }์คํ ๊ฒฐ๊ณผ
๋ถํ ํ 1234์ ๋ ฅํ์ฌ ๋ก๊ทธ์ธ [๊ฒ์์ ํ๋ค.] ์์ ์ข ๋ฃ ์ปดํจํฐ ์ข ๋ฃ aop01.Proxy์ด์ฉ์๊ฐ:100
[Main] -> [Proxy] -> [Program] ์์๋ก ์งํ๋๋ฉฐ usingComputer()๊ฐ ์คํ๋๊ธฐ ์ ์๋ ๋ฌด์กฐ๊ฑด Proxy์ ์ถ๋ ฅ๋ฌธ์ด ์คํ๋๋ค.
2. aspect ์ด์ฉ
spring์์ ์ ๊ณตํ๋ aspect๋ฅผ ํตํด ํน์ ์กฐ๊ฑด์ ์ค์ ํ ํ before, after ๋ฑ์ ํตํด์ ์ด๋ค ์์ (joinPoint)์ ํ์๋ฅผ ์คํํ ๊ฒ์ธ์ง ์ค์ ํ ์ ์๋ค.
- xml์ ์์ ํ๋ค.
pom.xml์ aop์ ๋ํ dependency๋ฅผ ์ถ๊ฐํ๋ค.<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.0.8.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency>- ์ค์ xml ํ์ผ์ aop ํ๊ทธ๋ฅผ ๋ฃ๋๋ค.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean ๊ฐ์ฒด ์ค @Aspect ๋ฅผ ์ฐพ์ aop๋ก ์ธ์งํ๋ค. --> <aop:aspectj-autoproxy /> <bean id="myAspect" class="aop02.MyAspect" /> <bean id="programmer" class="aop02.Programmer" /> </beans>
- aop๋ฅผ ์ฌ์ฉํ ํด๋์ค๋ฅผ ์์ฑํ๋ค.
@Aspect public class MyAspect { @Before("execution(public int aop02.*.usingComputer(String))") public void bootingAndLogin(JoinPoint jp) { String pwd = (String) jp.getArgs()[0]; System.out.println("์ปดํจํฐ ๋ถํ ์์. " + pwd + " ์ ๋ ฅ ๋ก๊ทธ์ธ"); } }@Before- ์ง์ ํ ๋ฉ์๋๊ฐ ์คํ๋๊ธฐ ์ ์ ํ๋จ์ ๋ฉ์๋๋ฅผ ์คํํ๋ค. (Advice types์ ๋ํ ์ ๋ฆฌ ๋ณด๊ธฐ)"execution(* usingComputer(..))"- ๋งค๊ฐ๋ณ์๋ก ๋ฌด์์ ๋ฐ๋ ๋ชจ๋ ํด๋์ค์ usingComputer() ๋ฉ์๋๋ฅผ ์คํ ์ง์ ์ผ๋ก ์ง์ ํ๋ค.JoinPoint- joinํ ์ง์ ์ ์๋ฏธํ๋ค.- join๋ ์ง์ ์์ ์คํ๋ ๋ฉ์๋์ ์ ๋ฌ๋ ๋งค๊ฐ๋ณ์ ๋ฑ ๋ค์ํ ์ ๋ณด๋ฅผ ์ป์ ์ ์๋ค.
getArgs()- ์ ๋ฌ๋ ๋งค๊ฐ๋ณ์๋ฅผ Object[]๋ก ๋ฐํํ๋ค.
- ์คํํ์ฌ ๊ฒฐ๊ณผ๋ฅผ ํ์ธ
- Person interface๋ฅผ ๊ตฌํํ Programmer ํด๋์ค ์์ฑ
public class Programmer implements Person{ public int usingComputer(String pwd) { try { //ํต์ฌ๊ธฐ๋ฅ System.out.println("[์ฝ๋ฉ์ ํ๋ค.]"); }catch(Exception e) { if(e.getMessage().equals("์ค๋ฅ!")) { System.out.println("AS๋ฅผ ์ ์ฒญํ๋ค."); } } System.out.println("์ปดํจํฐ ์ข ๋ฃ"); return 200; } } - main
public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("config.xml"); // interface๋ฅผ ๊ตฌ์ฒดํํ๊ธฐ ๋๋ฌธ์ ์์ ํด๋์ค๋ก get์ ํด์ผํ๋ค. Person p1 = ctx.getBean("programmer", Person.class); int useT1 = p1.usingComputer("1234"); System.out.println(p1.getClass().getName() + "์ด์ฉ์๊ฐ:" + useT1); } } - ์คํ ๊ฒฐ๊ณผ
์ปดํจํฐ ๋ถํ ์์. 1234 ์ ๋ ฅ ๋ก๊ทธ์ธ [์ฝ๋ฉ์ ํ๋ค.] ์ปดํจํฐ ์ข ๋ฃ com.sun.proxy.$Proxy5์ด์ฉ์๊ฐ:200
- Person interface๋ฅผ ๊ตฌํํ Programmer ํด๋์ค ์์ฑ
