5-3 配置切入点Pointcut

在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课程 整理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring AOP(面向切面编程)中,Pointcut是一个匹配执行特定Java方法或代码段的规则。如果你想在Spring AOP中设置多个切入点,你可以定义多个不同的Pointcut表达式,每个表达式对应一个特定的方面(Advice)。这些表达式可以用来定义不同类型的横切关注点,比如业务逻辑验证、性能监控等。 设置多个切入点的过程通常是这样的: 1. **定义Pointcut表达式**: - 使用`@Before`, `@After`, `@Around`注解声明一个切面类(Aspect),并在其中定义`@Pointcut`注解的字符串形式的Pointcut表达式。 ```java @Pointcut("execution(* com.example.service.*.*(..))") // 匹配所有service包下的方法 private void serviceOperation() {} @Pointcut("execution(* com.example.repository.*.*(..))") // 匹配所有repository包下的方法 private void repositoryOperation() {} ``` 2. **编写Advice**: - 在切面类中编写实际的增强逻辑,针对每个切入点分别处理。例如,可以在`@Around`注解的方法中根据Pointcut选择执行相应的代码。 ```java @Around("serviceOperation()") // 增强service操作 public Object adviceForService(ProceedingJoinPoint joinPoint) throws Throwable { // ... } @Around("repositoryOperation()") // 增强repository操作 public Object adviceForRepository(ProceedingJoinPoint joinPoint) throws Throwable { // ... } ``` 3. **关联切点和通知**: - 在切面配置中,使用`@Component`注解将切面组件化,并通过`@Aspect`注解标识为切面。 - 使用`@Autowired`注入到Spring容器中,让Spring自动关联切点和通知。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值