基于注解配置的AOP

注解方式AOP基本使用

Spring的AOP也提供了注解方式配置,使用相应的注解代替之前的xml配置。

xml配置:

    <aop:config>
<!--        配置切入点  目的是指定哪些方法增强-->
        <aop:pointcut id="myPointCut1" expression="execution(void com.hsf.service.Impl.UserServiceImpl.show01())"/>
        <aop:aspect ref="advice">
            <aop:before method="beforeAdvice" pointcut-ref="myPointCut1"></aop:before>
        </aop:aspect>
    </aop:config>

注解配置:

<!--    注解包扫描-->
    <context:component-scan base-package="com.hsf"></context:component-scan>
<!--    配置AOP注解自动代理  -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
@Component("myadvice")
@Aspect
public class MyAdvice {
    //增强方法
    @Before("execution(void com.hsf.service.Impl.UserServiceImpl.show01())")
    public void beforeAdvice(JoinPoint joinPoint){
        System.out.println("当前目标对象"+joinPoint.getTarget());
        System.out.println("表达式"+joinPoint.getStaticPart());
        System.out.println("前置的增强");
    }

 

注解方式AOP配置详解
//前置增强方法
    @Before("execution(void com.hsf.service.Impl.UserServiceImpl.show01())")
      //后置通知
    @AfterReturning(pointcut = "execution(void com.hsf.service.Impl.UserServiceImpl.show01())")
    //环绕通知
    @Around( "execution(void com.hsf.service.Impl.UserServiceImpl.show01())")
     //异常抛出通知
    @AfterThrowing(pointcut ="execution(void com.hsf.service.Impl.UserServiceImpl.show01())", throwing = "e")
        //最终通知
    @After("execution(void com.hsf.service.Impl.UserServiceImpl.show01())")

  切点表达式的抽取:

  //切点表达式的抽取
    @Pointcut
    public void myPointCut(){
        
    }

调用抽取的表达式: 

 配置类,全注解开发

@Configuration
@ComponentScan("com.hsf")//  <context:component-scan base-package="com.hsf"></context:component-scan>
@EnableAspectJAutoProxy//<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
public class SpringConfig {
}

 

注解方式AOP原理剖析

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值