복붙노트

[SPRING] 스프링 3에서 컨트롤러 용 AOP 구현

SPRING

스프링 3에서 컨트롤러 용 AOP 구현

주석이 달린 컨트롤러로 AOP를 구현하려면 어떻게해야합니까?

나는이 문제에 관한 두 가지 이전 게시물을 검색하고 발견했지만 해결책을 얻을 수없는 것 같습니다.

게시 된 솔루션 1

게시 된 솔루션 2

여기에 내가 가지고있는 것이있다.

디스패치 서블릿 :

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.foo.controller"/>

    <bean id="fooAspect" class="com.foo.aop.FooAspect" />

    <aop:aspectj-autoproxy>
        <aop:include name="fooAspect" />
    </aop:aspectj-autoproxy>
</beans>

제어 장치:

@Controller
public class FooController {

    @RequestMapping(value="/index.htm", method=RequestMethod.GET)
    public String showIndex(Model model){
        return "index";
    }
}

양상:

@Aspect
public class FooAspect {

    @Pointcut("@target(org.springframework.stereotype.Controller)")
    public void controllerPointcutter() {}

    @Pointcut("execution(* *(..))")
    public void methodPointcutter() {}

    @Before("controllerPointcutter()")
    public void beforeMethodInController(JoinPoint jp){
        System.out.println("### before controller call...");
    }

    @AfterReturning("controllerPointcutter() && methodPointcutter() ")
    public void afterMethodInController(JoinPoin jp) {
        System.out.println("### after returning...");
    }

    @Before("methodPointcutter()")
    public void beforeAnyMethod(JoinPoint jp){
        System.out.println("### before any call...");
    }
}

beforeAnyMethod ()는 컨트롤러가 아닌 메소드에서 작동합니다. 컨트롤러에 대한 호출을 실행할 수있는 어떤 것도 얻을 수 없습니다. 내가 놓친 게 있니?

해결법

  1. ==============================

    1.Spring 3.1에서 @Controller로 주석 처리 된 클래스에서 HandlerMethod에 aspect를 적용하려면 aspectj-autoproxy 요소에 proxy-target-class = "true"속성이 있어야한다. 또한 WAR / EAR 파일에 CGLIB 및 ASM 라이브러리가 있어야합니다. 주석이 달린 애스펙트 클래스를 Bean으로 지정하고 위에서 설명한대로 aop : include를 사용하거나 aop을 빠져 나올 수 있습니다 : include를 포함하고 이와 유사한 필터를 구성 요소 스캔 요소에 추가합니다.

    Spring 3.1에서 @Controller로 주석 처리 된 클래스에서 HandlerMethod에 aspect를 적용하려면 aspectj-autoproxy 요소에 proxy-target-class = "true"속성이 있어야한다. 또한 WAR / EAR 파일에 CGLIB 및 ASM 라이브러리가 있어야합니다. 주석이 달린 애스펙트 클래스를 Bean으로 지정하고 위에서 설명한대로 aop : include를 사용하거나 aop을 빠져 나올 수 있습니다 : include를 포함하고 이와 유사한 필터를 구성 요소 스캔 요소에 추가합니다.

    <context:component-scan>
      <context:include-filter type="aspectj"
        expression="com.your.aspect.class.Here"/>
    </context:component-scan>
    

    이것이 Spring 3.1의 결과로서의 요구 사항인지는 모르겠다. 그러나 이것 없이는 컨트롤러 HandlerMethod에 aspect를 넣을 수 없을 것이다. 다음과 유사한 오류가 발생합니다.

    Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    HandlerMethod details: 
    Controller [$Proxy82]
    Method [public void com.test.TestController.testMethod(java.security.Principal,javax.servlet.http.HttpServletResponse) throws javax.servlet.ServletException]
    Resolved arguments: 
    [0] [null] 
    [1] [type=com.ibm.ws.webcontainer.srt.SRTServletResponse] [value=com.ibm.ws.webcontainer.srt.SRTServletResponse@dcd0dcd]
    

    애스펙트가 컨트롤러가 아닌 클래스의 메소드에있는 경우에는 필요하지 않습니다.

  2. ==============================

    2.나는 대체 해결책 (미안하지만 직접적인 대답)을 말하려고 하겠지만, 당신이하고 싶은 것은 아마도 인터셉터와 필터를 통해 가장 잘 수행 될 것이다.

    나는 대체 해결책 (미안하지만 직접적인 대답)을 말하려고 하겠지만, 당신이하고 싶은 것은 아마도 인터셉터와 필터를 통해 가장 잘 수행 될 것이다.

  3. from https://stackoverflow.com/questions/5305938/implement-aop-for-controllers-in-spring-3 by cc-by-sa and MIT license