springmvc+spring架构,aop无法拦截解决方案

项目是springmvc+spring,发现只能拦截dao层里方法

 

Service层不拦截将导致无法进行事务管理

Service层不拦截原因:springmvc的配置文件及spring的配置文件同时扫描了service,就无效了

Service层不拦截解决方案:将springmvc配置文件里将service层排除即可,如

    <!-- 配置扫描的包 -->
     <context:component-scan base-package="com.xxx.xxx" >   
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />   
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />    
    </context:component-scan>  

Controller层不拦截原因:切面配置必须与扫描controller层的配置放在同一个配置文件中

Controller层不拦截解决方案:将配置放一起,如在springmvc.xml中如下配置:
    <!-- 配置扫描的包 -->
     <context:component-scan base-package="com.xxx.xxx" >   
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />   
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />    
    </context:component-scan>  

<!-- 切面配置-->
    <bean id="restfulAop" class="com.xxx.xxx.fitter.RestfulAop"/>
    <aop:config expose-proxy="true">
        <aop:aspect ref="restfulAop">
            <aop:pointcut id="restfulPointcut" expression="execution(* com.xxx.xxx.restful.*RestController.*(..))"/> 
            <aop:before pointcut-ref="restfulPointcut" method="before"/>
            <aop:after pointcut-ref="restfulPointcut" method="after"/>
        </aop:aspect>
    </aop:config>
springmvc.xml头部beans的属性里加上
xmlns:aop="http://www.springframework.org/schema/aop"
springmvc.xml头部beans的属性的xsi:schemaLocation里加上
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
posted on 2018-05-08 17:05  减肥少年  阅读(1519)  评论(0编辑  收藏  举报