spring aspect 用法 , 及整合springmvc 通知在service层不生效问题

问题如标题所示

applicationContext.xml

 <!--开启注解扫描,之扫描spring,mybatis,不扫描springmvc-->
    <context:component-scan base-package="com.nextStep">
        <!--配置那些注解不扫描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--配置aop-aspectj拦截注解生效-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

springmvc.xml

 <!--开启注解扫描,之扫描Controller注解-->
    <context:component-scan base-package="com.nextStep">
        <!--配置之扫描该注解,与applicationContext相反-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

aspectTest.java


@Component
@Aspect
public class UserAspect {

  
    @Before("execution(* com.nextStep.service..*(..))")
    public void befor(JoinPoint joinPoint) throws IOException {
        joinPoint.getSignature().getName();    //获得调用方法名
        System.out.println("前置通知生效");
    }

    @AfterReturning("execution(* com.nextStep.service..*(..))")
    public void after(JoinPoint joinPoint){

    }

    @AfterThrowing(value = "execution(* com.nextStep.service..*(..))" ,throwing = "e")
    public void aftherThread(Exception e){
        System.out.println("异常通知"+e.getMessage());
    }

}

尝试拦截service层的方法时,发现好像,拦截不成功,然后把@Before注解中的service改为dao,发现拦截成功了

那么说明这个写法,是没有问题的

几经纠结,发现在springmvc.xml中需要开启代理

至于为什么要这么做,我也 I don't no ,只管会用就可以了。

springmvc.xml

 <!--开启注解扫描,之扫描Controller注解-->
    <context:component-scan base-package="com.nextStep">
        <!--配置之扫描该注解,与applicationContext相反-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>    

<!--开启动态代理-->
    <aop:aspectj-autoproxy proxy-target-class="true" />

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值