5-3 配置切入点Pointcut

本文详细介绍了AOP(面向切面编程)在Spring框架中的应用,包括通知(Advice)与切入点表达式的关联,以及execution、within、this、target等表达式的使用。通过实例展示了如何配置切面、定义切入点,以及匹配指定类型内的方法执行。

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

在AOP中通知Advice和一个切入点表达式关联
execution用于匹配方法执行的连接点

pointcut

execution: AspectJ与Spring AOP 都支持:

  • execution(public **(…)) 切入点为执行所有public方法时
  • execution(* set*(..)) 切入点为执行所有set开始的方法时
  • execution(* com.xyz.service.AccountService.*(..)) 切入点为执行AccountService类中的所有方法时
  • execution(* com.xyz.service..(..)) 切入点为执行com.xyz.service包下的所有方法时
  • execution(* com.xyz.service…(. .)) 切入点为执行com.xyz.service包及其子包下的所有方法时

以下只有Spring AOP才支持

  • within(com.xyz.service.*) (only in Spring AOP)
  • within(com.xyz.service..*) (only in Spring AOP)
    within 用于匹配指定类型内的方法执行
  • this(com.xyz.service.AccountService) (only in Spring AOP)
    this 用于匹配当前AOP代理对象类型的执行方法
  • targer(com.xyz.service.AccountService) (only in Spring AOP)
    targer 用于匹配当前目标对象类型的执行方法
  • args(java.io.Serializable) (only in Spring AOP)
    @target(org.springframework.transaction.annotation.Transactional) (only in Spring AOP)
    args 用于匹配当前执行的方法传入的参数为指定类型的执行方法
  • @within(org.springframework.transaction.annotation.Transactional) (only in Spring AOP)
  • @annotation(org.springframework.transaction.annotation.Transactional) (only in Spring AOP)
  • @args(com.xyz.security.Classified) (only in Spring AOP)
  • bean(tradeService) (only in Spring AOP)
  • bean(*Service) (only in Spring AOP)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="moocAspect" class="com.torey.aop.schema.advice.MoocAspect"></bean>
    <bean id="aspectBiz" class="com.torey.aop.schema.advice.biz.AspectBiz"></bean>
    <aop:config>
        <aop:aspect id="moocAspectAOP" ref="moocAspect">
            <!--匹配AspectBiz类下的所有方法-->
            <aop:pointcut id="aspec" expression="execution(com.torey.aop.schema.advice.biz.AspectBiz.*(..))"/>
            <!--匹配以类名以Biz结尾的类的所有方法-->
           <!-- <aop:pointcut id="aspec" expression="execution(com.torey.aop.schema.advice.biz.*Biz.*(..))"/>-->
        </aop:aspect>
    </aop:config>
</beans>

根据慕课网 moocer老师的spring课程 整理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值