搜索
java - Aspectj在Controller中失效?
巴扎黑
巴扎黑 2017-04-17 14:28:13
[Java讨论组]

首先给出我的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>

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

巴扎黑
巴扎黑

全部回复(2)
黄舟

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

高洛峰

改完之后

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

改之前

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

<context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Service" />
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板