如何使用外部jar类的spring aop

时间:2016-11-02 01:01:19

标签: java spring spring-mvc aop spring-aop

我需要拦截org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter #handle方法,我使用spring aop。它是spring-webmvc.jar的春季课程。它在请求处理我的其余mvc服务期间在org.springframework.web.servlet.DispatcherServlet中使用。

AOP配置:

<bean id="contextAspect" class="com.eightbitlab.spring3rest.config.ContextAspect"/>

    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <aop:config>
        <aop:aspect ref="contextAspect">
            <aop:pointcut id="contextPointcut" expression="execution (* org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handle(..))"/>
            <aop:around method="around" pointcut-ref="contextPointcut"/>
        </aop:aspect>
    </aop:config>

Aspect bean:

@Aspect
public class ContextAspect {
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        Logger.getAnonymousLogger().info("start aspect");
        Object proceeded = joinPoint.proceed();
        Logger.getAnonymousLogger().info("end aspect");
    }
}

如果我将此方面与自己的类一起使用,则执行aspect方法。我认为问题是来自外部jar的类,但我找不到解决方案......

1 个答案:

答案 0 :(得分:0)

如果您查看handle方法的签名,则会将其标记为final

由于Spring AOP无法建议final方法(请参阅以下摘自Spring docs here ),请考虑拦截/建议您的控制器。

  

当您使用Spring MVC时, 无法 最终 方法添加建议。例如,您无法向AbstractController.setSynchronizeOnSession()方法添加建议。有关AOP代理的更多信息以及无法向最终方法添加建议的原因,请参阅Section 10.6.1,“了解AOP代理”。

P.S。:另外看看here以便进一步了解AOP代理。