Aspectj在Controller中失效?

首先给出我的AOP切面在ApplicationContext中的定义:

XML<aop:aspectj-autoproxy proxy-target-class="true" />
    <!-- aop logger -->
    <bean id="sbeatLog" class="sbeat.util.helper.SbeatLog" />
    <aop:config>
        <aop:aspect id="logAspect" ref="sbeatLog">
            <aop:pointcut expression="execution(* sbeat.service.*.*(..))" id="sbeatLogPointcut"/>       
            <aop:before pointcut-ref="sbeatLogPointcut" method="doBefore"/>
            <aop:around pointcut-ref="sbeatLogPointcut" method="doAround"/>
            <aop:after pointcut-ref="sbeatLogPointcut" method="doAfter"/>
        </aop:aspect>
</aop:config>

然后切面程序在Unit test里运行正常,但是到了controller里就毫无反应,经过排查,我发现把dispatcher-sevlet.xml里的一句话改动就修复了:

XML之前
<context:component-scan base-package="sbeat">
        <!-- 只扫描Controller -->
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
        <context:include-filter type="annotation"
            expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>

之后
<context:component-scan base-package="sbeat.controller">
        <!-- 只扫描Controller -->
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
        <context:include-filter type="annotation"
            expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan>

但有哪位高人告诉我是怎么回事?

阅读 4.9k
2 个回答

估计是被spring的parent context给覆盖了。

改完之后

仅仅扫描sbeat.controller包下用@Controller注解的类,那当然符合你的预期喽。

改之前

扫描的是sbeat下的所有类,包括@Controller,@Service,@Repository等注解的类,如果想去除@Service注解的类,可以使用

<context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Service" />
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题