티스토리 뷰
AOP (Aspect Oriented Programming) : 관점 지향 프로그래밍
- 공통 기능을 핵심 기능과 분리해 놓고, 공통 기능 중에서 핵심 기능에 적용하고자 하는 부분에 적용
기본 용어
1. Aspect : 공통 기능
2. Advice : Aspect 순수 기능 그 자체
3. Joinpoint : Advice를 적용해야 되는 부분 (메소드)
4. Pointcut : Joinpoint의 부분, 실제로 Advice가 적용된 부분
5. Weaving : Advice를 핵심 기능에 적용하는 행위
XML 기반 AOP 구현
0. 데이터 클래스
- Student.java
package com.spring.ex;
public class Student {
private String name;
private int age;
private int gradeNum;
private int classNum;
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;
}
public void getStudentInfo() {
System.out.println("이름 : " + getName());
System.out.println("나이 : " + getAge());
System.out.println("학년 : " + getGradeNum());
System.out.println("반 : " + getClassNum());
}
}
- Worker.java
package com.spring.ex;
public class Worker {
private String name;
private int age;
private String job;
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;
}
public void getWorkerInfo() {
System.out.println("이름 : " + getName());
System.out.println("나이 : " + getAge());
System.out.println("직업 : " + getJob());
}
}
1. 의존 설정 (pom.xml)
<!-- AOP -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId> aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
의존 설정한 것이 들어갔는지 확인
2. 공통 기능의 클래스 제작
- Advice 역할
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 starting");
long st = System.currentTimeMillis(); // start time
try {
//핵심 기능
Object obj = joinpoint.proceed();
return obj;
} finally {
long et = System.currentTimeMillis(); // end time
System.out.println(signatureStr + " is finished");
System.out.println(signatureStr + "경과 시간 : " + (et - st));
}
}
}
3. XML설정 파일에 Aspect 설정
<?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.spring.ex.LogAop" />
<aop:config>
<aop:aspect id="logger" ref="logAop"> <!-- 공통 기능 -->
<aop:pointcut expression="within(com.spring.ex.*)"
id="publicM" />
<aop:around pointcut-ref="publicM" method="loggerAop" />
</aop:aspect>
</aop:config>
<bean id="student" class="com.spring.ex.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.spring.ex.Worker">
<property name="name" value="홍길순" />
<property name="age" value="25" />
<property name="job" value="개발자" />
</bean>
</beans>
4. Main
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Main {
public static void main(String[] args) {
AbstractApplicationContext ctx = new GenericXmlApplicationContext("classpath:CTX.xml");
Student student = ctx.getBean("student", Student.class);
student.getStudentInfo();
Worker worker = ctx.getBean("worker", Worker.class);
worker.getWorkerInfo();
ctx.close();
}
}
5. 실행
JAVA 기반 AOP 구현
0. 데이터 클래스
- Student.java
- Worker.java
1. 의존 설정 (pom.xml)
2. @Aspect Annotation을 이용한 Aspect 클래스 구현
package com.spring.java;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
//공통 기능 클래스
@Aspect
public class LogAop {
// 공통 기능
@Pointcut("within(com.spring.java.*)")
private void pointcutMethod() {
// 범위 지정을 위해 생성 - 내용 없음
}
@Around("pointcutMethod()") // 공통 기능이 어디서 실행 될지 지정
public Object loggerAop(ProceedingJoinPoint joinpoint) throws Throwable {
String signatureStr = joinpoint.getSignature().toShortString();
System.out.println(signatureStr + " is starting");
long st = System.currentTimeMillis(); // start time
try {
//핵심 기능
Object obj = joinpoint.proceed();
return obj;
} finally {
long et = System.currentTimeMillis(); // end time
System.out.println(signatureStr + " is finished");
System.out.println(signatureStr + "경과 시간 : " + (et - st));
}
}
}
3. XML 파일 설정
<?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">
<aop:aspectj-autoproxy />
<bean id="logAop" class="com.spring.java.LogAop" />
<bean id="student" class="com.spring.java.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.spring.java.Worker">
<property name="name" value="홍길순" />
<property name="age" value="25" />
<property name="job" value="개발자" />
</bean>
</beans>
프록시 (Proxy) : AOP 구현 방법
- Advice가 직접 구현하는게 아니라, 프록시를 통해 구현
- Client(호출부)와 Target(핵심기능) 사이에서 대행 역할
- 공통 기능 수행
- PC의 핵심 기능에 접근
AspectJ Pointcut 표현식
- * : 모든
- . : 현재
- .. : 0개 이상
'BACK-END > SPRING' 카테고리의 다른 글
[SPRING] MVC 게시판 (0) | 2019.09.25 |
---|---|
[SPRING] MYBATIS MYSQL 연동 (0) | 2019.09.25 |
[SPRING] IoC - SPRING CONTAINER (0) | 2019.09.25 |
[SPRING] FRAMEWORK (0) | 2018.05.15 |
[SPRING] BASIC THINGS (0) | 2018.05.08 |