AOP 구현(XML 스키마 기반)
AOP 구현의 방식 중 XML 스키마를 기반으로 한 예제이다.
예제)
1. pom_xml(의존설정)
1 2 3 4 5 6 7 | <!-- AOP --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.4</version> </dependency> | cs |
2. Log.Aop.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.java.ex1; import org.aspectj.lang.ProceedingJoinPoint; public class LogAop { public Object loggerAop(ProceedingJoinPoint joinpoint) throws Throwable { String signatureStr = joinpoint.getSignature().toShortString(); System.out.println(signatureStr + "is start."); long st = System.currentTimeMillis() try { Object obj = joinpoint.proceed(); return obj; } finally { long et = System.currentTimeMillis(); System.out.println(signatureStr + " is finished."); System.out.println(signatureStr + " 경과시간 : " + (et - st)); } } } | cs |
3. Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | package com.java.ex1; public class Student { private String name; private int age; private int gradeNum; private int classNum; public void getStudentInfo() { System.out.println("이름 : " + getName()); System.out.println("나이 : " + getAge()); System.out.println("학년 : " + getGradeNum()); System.out.println("반 : " + getClassNum()); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getGradeNum() { return gradeNum; } public void setGradeNum(int gradeNum) { this.gradeNum = gradeNum; } public int getClassNum() { return classNum; } public void setClassNum(int classNum) { this.classNum = classNum; } } | cs |
4. Worker.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.java.ex1; public class Worker { private String name; private int age; private String job; public void getWorkerInfo() { System.out.println("이름 : " + getName()); System.out.println("나이 : " + getAge()); System.out.println("직업 : " + getJob()); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } } | cs |
5. MainClass.Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.java.ex1; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; public class MainClass { public static void main(String[] args) { AbstractApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationCTX1.xml"); Student student = ctx.getBean("student", Student.class); student.getStudentInfo(); Worker worker = ctx.getBean("worker", Worker.class); worker.getWorkerInfo(); ctx.close(); } } | cs |
6. applicationCTX1.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <bean id="logAop" class="com.java.ex1.LogAop" /> <aop:config> <aop:aspect id="logger" ref="logAop"> <aop:pointcut expression="within(com.java.ex1.*)" id="publicM" /> <aop:around method="loggerAop" pointcut-ref="publicM" /> </aop:aspect> </aop:config> <bean id="student" class="com.java.ex1.Student"> <property name="name" value="홍길동" /> <property name="age" value="10" /> <property name="gradeNum" value="3" /> <property name="classNum" value="5" /> </bean> <bean id="worker" class="com.java.ex1.Worker"> <property name="name" value="홍길순" /> <property name="age" value="35" /> <property name="job" value="개발자" /> </bean> </beans> | cs |
=> 관점 지향 프로그래밍 AOP 구현을 위해 pom_xml에서 의존설정을 해주었다.
관점 지향 프로그래밍에 맞게 공통 기능과 핵심 기능으로 기능을 분리한다.
공통 기능은 공통기능이 필요한 핵심 기능의 부분에만 적용할 것이기 때문이다.
LogAop.java 에 작성된 코드가 공통 기능에 해당하고, 이를 applicationCTX1.xml에
추가하였다. <bean id="logAop" class="com.java.ex1.LogAop" />
위의 코드를 보면 Advice(공통 기능 자체)가 around 로 지정되어있는 것을 볼 수 있다.
AOP구현에 있어서 Advice의 종류는 5가지가 있는데 그 중 around 는 메서드의 실행 전 / 후 및
exception 발생시 Advice를 실행한다. 따라서 실행결과를 보면 Student 와 Worker 가 print 되기
전 후로 공통 기능(LogAop)이 실행된 것을 확인 할 수 있다.
Advice의 종류
<aop:before> : 메소드 실행 전에 advice실행
<aop:after-returning> : 정상적으로 메소드 실행 후에 advice실행
<aop:after-throwing> : 메소드 실행중 exception 발생시 advice실행
<aop:after> : 메소드 실행중 exception 이 발생하거나 하지 않아도 advice실행
<aop:around> : 메서드 실행 전/후 및 exception 발생시 advice실행