본문 바로가기

spring/AOP

AOP 구현하는 방법3 : 어노테이션을 이용한 방법

할말이 읎다


==============================================================================================================================================

설정파일


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

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 일반 객체 생성(DI) -->

<bean id="logicImpl" class="aop3_anno.LogicImpl">

<constructor-arg>

<ref bean="articleDao"/>

</constructor-arg>

</bean>

<bean id="articleDao" class="aop3_anno.ArticleDao"/>


<!-- @Aspect 사용 시 설정 -->

<aop:aspectj-autoproxy/>


<bean id="profileAdvice" class="aop3_anno.ProfileAdvice"/>

</beans>


==============================================================================================================================================


package aop3_anno;


import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;


@Aspect

public class ProfileAdvice { //Advice용

// @Around : @Around 어노테이션에 해당하는 메소드의 실행 전 또는 실행 후를 처리할 경우 사용

@Around("execution(public * aop3_anno..*(..))")

public Object kbs(ProceedingJoinPoint joinPoint) throws Throwable{

String methodName = joinPoint.getSignature().toString(); //핵심 메소드명 얻기

System.out.println(methodName + " 시작 전 작업....");

Object object = joinPoint.proceed(); //핵심 메소드

System.out.println(methodName + " 종류 후 작업....");

return object;

}

// @Before : @Before 어노테이션에 명시된 메소드 실행전만 처리할 경우 사용

@Before("execution(public * aop3_anno..*(..))")

public void before(){

System.out.println("메소드 실행 전 @Before");

}

// @After : @After 어노테이션에 명시된 메소드 실행후만 처리할 경우 사용

@After("execution(public * aop3_anno..*(..))")

public void after(){

System.out.println("메소드 실행 후 @After");

}

}