티스토리 뷰

반응형

 

 

 

Spring Framework  사용시에 전체 Serivce 와 Controller 혹은 특정 Service와 Controller에 

일괄적인 작업을 해줄 필요가 있을 때가 있다.

경우에 따라 다르겠지만 Log와 같은 경우, 전체 Controller에 특정 값을 셋팅 해줄 경우가 이에 

해당하게 되겠네요.


Spring에서 Aop를 설정하기 위한 방법은 몇가지 있지만 그중에서 가장 구현하기 간단한

Annotation을 이용한 방법을 사용하려 합니다.


먼저 servlet-context.xml 혹은 dispatcherServlet.xml 과 같이 web.xml에 설정하신 

servlet 설정 파일에 아래와 같은 Bean을 등록하여 줍니다.


 

1
2
<aop:aspectj-autoproxy/>
<bean id="testAspect" class="com.xxx.xxx.xxx.xxx.testAspect" />
cs


위와 같이 등록하신 후에 Bean를 구현하여 줍니다.


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.xxx.xxx.xxx.aop;
 
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
 
@Aspect
public class testAspect {
    
    /**
     * @param joinPoint
     * @return Object
     * @throws Throwable
     */
    @Around("execution(* com.xxx.xxx.xxx..*.*Controller.*(..))")
    public Object setMapParamter(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("Controller 실행시에 출력된다.");
        return joinPoint.proceed();
    }
}
cs


위와 예제와 같이 @Aspect 어노테이션을 설정하시고 ProceedingJoinPoint joinPoint 을 

파라미터로 받으신 후에 실제 사용할 Bean을 구현하시면 됩니다.

위의 예제에서는 문구를 출력하는 간단한 형식으로 구현하였습니다.


상단에 @Around Advice와 ("execution(* com.xxx.xxx.xxx..*.*Controller.*(..))"

라고 기술 되어 있는 포인트 컷의 경우는 Aop 설정중에 @Before @After, @AfterThrowing

같은 어노테이션을 사용하셔서 포이트컷에 설정된 target의 실행전, 실행후, 에러

발생시... 등과 같은 상황에 필요에 따라서 개입할 수 있게 만들어 주는 어노테이션입니다.

현제 포인트 컷은 com.xxx.xxx.xxx 의 하위 패키지에서 Controller이 불러지면 실행 되도록

설정 되어 있습니다.


포인트 컷이나 어드바이스에 대한 내용을 좀 더 알고 싶으시면 


http://www.egovframe.go.kr/wiki/doku.php?id=egovframework:rte:fdl:aop:aspectj

아래의 전자정부 프레임워크에 대한 설명에서 좀 더 상세히 아실 수 있습니다.


위와 같이 설정하신 후 was를 실행하시고 Controller를 실행 하시면 아래와 같은 결과를 보실 

수 있습니다.


 


이상입니다. 즐프되세요~~^^

반응형
반응형
최근에 올라온 글
«   2025/02   »
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