본문 바로가기

Programming/Spring

AOP 설정을 root-context 와 servlet-context 에 했을 때의 차이점

AOP 설정을 root-context 와 servlet-context 에 했을 때의 차이점



AOP 적용을 위해 구현한 Advice 를 component-scan 을 통해 불러와야 한다. component-scan 은 root-context 와 servlet-context 에 할 수 있는데, 위 두 context 간 계층이 존재하기 때문에 설정시 주의해야 한다. 


1. root-context 에서 scan 하여 추가된 component 는 servlet-context 에서 scan 하여 추가된 component 를 사용할 수 있다.


2. 반대로 servlet-context 에서 추가된 component 는 root-context 에서 추가된 component 를 사용할 수 없다.


따라서 아래와 같은 상황에서는 Advice 를 정의한 package 인 com.repacat.aop 를 root-context 에서 scan 해야 service, persistence, controller 에 있는 클래스에 적용할 수 있게 된다.


root-context.xml

1
2
3
    <context:component-scan base-package="com.repacat.persistence" />
    <context:component-scan base-package="com.repacat.service" />
    <context:component-scan base-package="com.repacat.aop" />
cs

servlet-context.xml

1
    <context:component-scan base-package="com.repacat.controller" />
cs



servlet-context.xml 에서는 com.repacat.controller  패키지에 있는 클래스를 로드하고 있다. 이 클래스에서는 처리해야할 URL 에 대한 @RequestMapping 정보와 처리 method 가 정의되어 있다. 이 내용은 dispatcherServlet 이 사용하는 것이기 때문에 이 클래스는 servlet-context.xml 에서 로드(scan) 되어야 한다.


쉽게 이해하자면 url 관련 설정을 담고 있는 클래스는 servlet-context.xml 에서 로드되어야 한다. (controller 외에 interceptor 도)




출처: http://repacat.tistory.com/32 [레파캣]




'Programming > Spring' 카테고리의 다른 글

[Spring] 현재 HttpServletRequest 가져오기  (0) 2015.05.20
[Spring] @ResponseBody 리턴시 Timestamp 깨짐 현상  (0) 2015.01.13
[Spring] @RequestParam  (0) 2015.01.13
[Spring] @RequestBody  (0) 2015.01.13
[Spring] @PathVariable  (0) 2015.01.09