180328 Spring AOP - RYUDONGJIN/Memo_wiki GitHub Wiki

AOP(Aspect Oriented Programing)

  • ์ƒ์†์„ ํ†ตํ•œ ๋ฐฉ๋ฒ•์— ํ•œ๊ณ„๊ฐ€ ์žˆ์–ด์„œ ๋“ฑ์žฅ, ํ•ต์‹ฌ๊ธฐ๋Šฅ๊ณผ ๊ณตํ†ต ๊ธฐ๋Šฅ์„ ๋ถ„๋ฆฌ ์‹œ์ผœ๋†“๊ณ , ๊ณตํ†ต ๊ธฐ๋Šฅ์„ ํ•„์š”๋กœ ํ•˜๋Š” ํ•ต์‹ฌ ๊ธฐ๋Šฅ๋“ค์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹
  • ๊ณตํ†ต ๊ธฐ๋Šฅ์„ ํ•ต์‹ฌ ๊ธฐ๋Šฅ๊ณผ ๋ถ„๋ฆฌํ•ด๋†“๊ณ , ๊ณตํ†ต ๊ธฐ๋Šฅ ์ค‘์—์„œ ํ•ต์‹ฌ ๊ธฐ๋Šฅ์— ์ ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ๋ถ€๋ถ„์— ์ ์šฉํ•˜๋Š” ๊ฒƒ
  • ํŠธ๋žœ์žญ์…˜, ๋กœ๊น…, ๋ณด์•ˆ๊ณผ ๊ฐ™์€ ์—”ํ„ฐํ”„๋ผ์ด์ฆˆ ์„œ๋น„์Šค๋ฅผ ์ ์šฉํ•˜๊ณ  ๊น”๋”ํ•œ ์ฝ”๋“œ๋ฅผ ์œ ์ง€
  • Aspect : ๊ณตํ†ต ๊ธฐ๋Šฅ
  • Advice : Aspect์˜ ๊ธฐ๋Šฅ ์ž์ฒด
  • Joinpoint : Advice๋ฅผ ์ ์šฉํ•ด์•ผ ๋˜๋Š” ๋ถ€๋ถ„(ex.ํ•„๋“œ, ๋ฉ”์†Œ๋“œ)
  • Pointcut : Joinpoint์˜ ๋ถ€๋ถ„์œผ๋กœ ์‹ค์ œ๋กœ Advice๊ฐ€ ์ ์šฉ๋œ ๋ถ€๋ถ„
  • Weaving : Advice๋ฅผ ํ•ต์‹ฌ ๊ธฐ๋Šฅ์— ์ ์šฉํ•˜๋Š” ํ–‰์œ„

Advice ์ข…๋ฅ˜

  • aop:around : ๋ฉ”์†Œ๋“œ ์‹คํ–‰ ์ „/ํ›„ ๋ฐ exception ๋ฐœ์ƒ์‹œ advice ์‹คํ–‰
  • aop:before : ๋ฉ”์†Œ๋“œ ์‹คํ–‰ ์ „์— advice ์‹คํ–‰
  • aop:after-returning : ์ •์ƒ์ ์œผ๋กœ ๋ฉ”์†Œ๋“œ ์‹คํ–‰ ํ›„์— advice ์‹คํ–‰
  • aop:after-throwing : ๋ฉ”์†Œ๋“œ ์‹คํ–‰ ์ค‘ exception ๋ฐœ์ƒ์‹œ advice ์‹คํ–‰
  • aop:after : ๋ฉ”์†Œ๋“œ ์‹คํ–‰ ์ค‘ exception์ด ๋ฐœ์ƒํ•˜์—ฌ๋„ advice ์‹คํ–‰

@Aspect๋ฅผ ์ด์šฉํ•œ AOP๊ตฌํ˜„

  • ์˜์กด ์„ค์ •(pom.xml)
  • @Aspect ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•œ Aspectํด๋ž˜์Šค ์ œ์ž‘
  • XMLํŒŒ์ผ์— aop:aspectj-autoproxy/ ์„ค์ •

AspectJ Pointcut ํ‘œํ˜„์‹

  • @Pointcut("execution(public void get*(..))")
  • @Pointcut("within(com.javalec.ex.*)")
  • @Pointcut("bean(student)")

Student.java

package com.javalec.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());
	}	
}

LogAop.java

package com.javalec.ex;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class LogAop {
	
	@Pointcut("within(com.javalec.ex.*)")
	private void pointcutMethod() {
	}
	
	@Around("pointcutMethod()")
	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));
		}
	}
	
	@Before("within(com.javalec.ex.*)")
	public void beforeAdvice() {
		System.out.println("before");
	}
}

applicationCTX.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.javalec.ex.LogAop"/>
	
	<bean id="student" class="com.javalec.ex.Student">
		<property name="name" value="ํ™๊ธธ๋™"/>
		<property name="age" value="10" />
		<property name="gradeNum" value="3"/>
		<property name="classNum" value="5"/>
	</bean>

</beans>

MainClass.java

package com.javalec.ex;

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:applicationCTX.xml");
		
		Student student = ctx.getBean("student", Student.class);
		student.getStudentInfo();
		
		ctx.close();
	}
}
โš ๏ธ **GitHub.com Fallback** โš ๏ธ