java-aop 通知 *与..的区别(execution和)

 //@Pointcut("execution(public void com.itxiaoli.service.impl.DeptServiceImpl.delete(java.lang.Integer))")
//表示使用全称的方式进行书写表达式,不进行任何的匹配,这个也是精确的写法
    //@Pointcut("execution(void com.itxiaoli.service.impl.DeptServiceImpl.delete(java.lang.Integer))")
    //@Pointcut("execution(void delete(java.lang.Integer))") 
//包名.类名不建议省略  public可以省略不写,如果是私有的记得写上
//包名类名如果省略的话,不同包下面的方法如果名称也是delete也会运行这个通知
    //@Pointcut("execution(void com.itxiaoli.service.DeptService.delete(java.lang.Integer))")

    //@Pointcut("execution(void com.itxiaoli.service.DeptService.*(java.lang.Integer))")
//使用*号可以进行任意一个字符的省略,例如:DeptService.*代表DeptService下面任意的方法
//注意*号仅仅表示一个参数
    //@Pointcut("execution(* com.*.service.DeptService.*(*))")
    //@Pointcut("execution(* com.itxiaoli.service.*Service.delete*(*))")

    //@Pointcut("execution(* com.itxiaoli.service.DeptService.*(..))")
//使用..进行省略通配的话,是通配任意字符,不限制数量
    //@Pointcut("execution(* com..DeptService.*(..))")
    //@Pointcut("execution(* com..*.*(..))")
    //@Pointcut("execution(* *(..))") //慎用,这样写也不是不行,但是很模糊后期不好维护

    @Pointcut("execution(* com.itxiaoli.service.DeptService.list()) || " +
            "execution(* com.itxiaoli.service.DeptService.deleteDeptById(..))")
//也可以使用||符号,精确的书写多个确定的方法
    private void pt(){}

    @Before("pt()")
    public void before(){
        log.info("MyAspect ... before ...");
    }

使用execution来匹配多个无规则的具体的方法比较困难

使用annotation注解就比较简单,@Pointcut("@annotation(com.example.aop.MyLog)")

这一串代码的意思是添加自定义注解MyLog的路径

//切面类
@Slf4j
@Aspect
@Component
public class MyAspect {

    //匹配DeptServiceImpl中的 list() 和 delete(Integer id)方法
    //@Pointcut("execution(* com.itheima.service.DeptService.list()) || execution(* com.itheima.service.DeptService.delete(java.lang.Integer))")
    @Pointcut("@annotation(com.example.aop.MyLog)")
    private void pt(){}

    @Before("pt()")
    public void before(){
        log.info("MyAspect ... before ...");
    }

}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyLog {
}

在新建类的时候选择注解即可(@)

添加上以后就可以看自己想要那个方法,显示在控制台,或者运行信息保存到日志文件。

会更灵活

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring 中,通过配置 `<aop:config>` 标签,可以设置 AOP 的暴露方式。具体来说,可以通过设置 `<aop:config>` 标签的 `expose-proxy` 属性来控制代理对象的暴露方式。`expose-proxy` 属性有以下两个取值: 1. false:表示不暴露代理对象,默认值为 false。 2. true:表示暴露代理对象。在使用暴露代理对象的方式时,需要将代理对象注入到 Spring 容器中,并且在需要使用代理对象的地方,通过 `AopContext.currentProxy()` 方法来获取当前的代理对象。 例如,以下是一个使用暴露代理对象的例子: ```xml <aop:config proxy-target-class="true" expose-proxy="true"> <!-- 定义切面 --> <aop:aspect ref="myAspect"> <!-- 定义切点 --> <aop:pointcut id="myPointcut" expression="execution(* com.example.service.*.*(..))"/> <!-- 定义通知 --> <aop:before method="before" pointcut-ref="myPointcut"/> </aop:aspect> </aop:config> <bean id="myService" class="com.example.service.MyServiceImpl"> <!-- 注入代理对象 --> <aop:scoped-proxy/> </bean> <bean id="myAspect" class="com.example.aspect.MyAspect"/> ``` 在上面的例子中,通过设置 `proxy-target-class="true"` 和 `expose-proxy="true"` 来启用代理对象的暴露。同时,使用 `<aop:scoped-proxy/>` 标签将代理对象注入到 Spring 容器中。在需要使用代理对象的地方,可以通过 `AopContext.currentProxy()` 方法来获取当前的代理对象,例如: ```java @Service public class MyServiceImpl implements MyService { @Override public void doSomething() { MyService proxy = (MyService) AopContext.currentProxy(); // 使用代理对象执行业务逻辑 } } ``` 需要注意的是,使用暴露代理对象的方式可能会存在一些问题,例如线程安全性问题。因此,在使用暴露代理对象的方式时,需要仔细考虑其可能带来的风险并进行相应的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值