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

本文详细介绍了在SpringMVC+Spring项目中如何正确配置AOP,解决Service层和Controller层的事务管理和切面拦截问题。通过调整扫描配置和切面定义,确保项目的正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目是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

转载于:https://www.cnblogs.com/jeanfear/p/9009371.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值