티스토리 뷰

AOP ( Aspect Oriented Programming ) : 관점 지향 프로그래밍

문제를 해결하기 위한 핵심적인 부분( CC : Core Concern )과, 전체에 적용되는 공통적인 부분( CCC : Cross Cutting Concern )을 나누어 프로그래밍함으로써 공통모듈을 여러 코드에 쉽게 적용할 수 있도록 지원하는 기술

공통적인 부분을 한데 모아 모듈화하여 핵심 로직으로부터 분리하고 해당 기능을 프로그램 코드에서 직접 명시하지 않고 선언하여 적용한다.

 

공통 관심사항을 분리하는 AOP를 적용

 

 

 

AOP 용어

  • CC (Core Concern) : 핵심 관심사항 ( = target )

  • CCC (Cross Cutting Concern) : 공통 관심사항

  • Advice : CCC의 실제구현체, 코드 ( Pointcut에서 지정한 JoinPoint에 삽입되어야 하는 코드 )

  • JoinPoint : CC를 호출하는 모든 시점, Advice가 연결될 수 있는 모든 시점

  • Pointcut : Advice가 어떤 JoinPoint에 적용되어야 하는지 정의 ( 어떤 CC를 호출할 때 묶어줄건지 알려주는 애 )

  • Aspect ( Advisor ) : 흩어진 관심사항들을 모듈화한 것 ( Advice + Pointcut )

  • proxy : CC(target)을 호출할 때 target인 척 하여 요청을 대신 받는 애

  • Weaving : Advice를 비즈니스 로직 코드에 삽입하는 것, Aspect를 대상 객체에 적용하여 새로운 프록시 객체를 생성하는 과정, 엮여서 실제로 실행된 것

 

 


예제로 이해하기

 

pom.xml : maven dependency 추가

	<dependencies>
    
		<!-- spring core -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>5.2.4.RELEASE</version>
		</dependency>
        
		<!-- spring context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.2.4.RELEASE</version>
		</dependency>
        
		<!-- spring aop -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>5.2.4.RELEASE</version>
		</dependency>

		<!-- aspectjweaver -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.9.2</version>
		</dependency>
        
		<!-- aspectjrt -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.9.2</version>
		</dependency>

	</dependencies>

 

 

Human.java 인터페이스

package com.test01;

public interface Human {

	String sayName(String name);
	String sayJob(String job);
}

 

 

Human을 상속받은 Person.java Class

package com.test01;

import org.springframework.stereotype.Component;

@Component
public class Person implements Human {

	private String name;
	private String job;

	public void setName(String name) {
		this.name = name;
	}

	public void setJob(String job) {
		this.job = job;
	}

	@Override
	public String sayName(String name) {
		System.out.println("나의 이름은 " + name + " 입니다.");
		return name;
	}

	@Override
	public String sayJob(String job) {
		System.out.println("나의 직업은 " + job + " 입니다.");
		return job;
	}

}

 

 

MyAdvice.java

package com.test01;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAdvice {

	public MyAdvice() {
	}

	@Before("execution(public String sayName(..))")
	public void beforeSaying(JoinPoint join) {
		System.out.println("당신의 이름은 무엇입니까?");
	}

	@After("execution(public String sayName(..))")
	public void afterSaying(JoinPoint join) {
		System.out.println("이름이 멋지시네요.");
	}

	@AfterReturning("execution(public String sayName(..))")
	public void afterReturnSaying(JoinPoint join) {
		System.out.println("직업이 무엇입니까?");
	}
}

 

 

MTest.java

package com.test01;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MTest {

	public static void main(String[] args) {
		ApplicationContext factory = new ClassPathXmlApplicationContext("com/test01/aopAppcontext.xml");

		Human person01 = factory.getBean("person", Human.class);
		person01.sayName("뽀로로");
		person01.sayJob("개발자");

		System.out.println("------------------------");

		person01.sayName("둘리");
		person01.sayJob("개그맨");

	}
}

 

 

aopAppcontext.xml ( Spring Bean Configuration File ) : namespace에 context, aop 추가하기

<?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:context="http://www.springframework.org/schema/context"
	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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	<context:component-scan base-package="com.test01"></context:component-scan>
	
<!-- 	<bean id="myAdvice" class="com.test01.MyAdvice"></bean>
	
	<aop:config>
		<aop:pointcut expression="execution(public * sayName(..))" id="namePoint"/>
		<aop:aspect ref="myAdvice">
			<aop:before method="beforeSaying" pointcut-ref="namePoint"/>
			<aop:after method="afterSaying" pointcut-ref="namePoint"/>
			<aop:after-returning method="afterReturnSaying" pointcut-ref="namePoint"/>
		</aop:aspect>
	</aop:config> -->

</beans>

 

 

실행결과

최근에 올라온 글
«   2024/07   »
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
Total
Today
Yesterday