spring中的切入点表达式

上篇中我们提到在XML文件中配置切入点,需要采用全匹配:

pointcut="execution(public void com.dimples.hehe.service.impl.CustomerServiceImpl.saveCustomer())"

这种全匹配的方式太繁杂,而且在实际开发中有数千种方法,通过这种方式肯定也不现实,下面我们讲讲切入点的通配符配法。

 

有时候,我们会在一个aspect中织入多个方法,这时候我们要写多个pointcut="execution(* com..*.*(..)"

这时候我们可以在外面定义好一个pointcut,然后在里面直接引用就好了(当然也可以在aspect标签里面定义,不过这样就只能在本标签内使用):

<aop:config>
                //这里定义了一个父切入点,下面可以直接引用
		<aop:pointcut expression="execution(* com.hehe.service.impl.*.*(..))" id="p1"/>
		<aop:aspect id="logAspect" ref="logger">
			<aop:before method="printLog" pointcut-ref="p1"/>
			<aop:before method="printLog" pointcut-ref="p1"/>
			<aop:before method="printLog" pointcut-ref="p1"/>
			<!-- <aop:after method="printLog1" pointcut-ref="p1"/> -->
			<!-- <aop:before method="printLog2" pointcut-ref="p1"/> -->
		</aop:aspect>
</aop:config>

 

### Spring AOP 切入点表达式使用方法 #### execution 表达式 `execution` 是最常用的切入点表达式之一,用于匹配特定的方法执行。其语法格式如下: ```plaintext execution([修饰符] 返回值 包名.类名.方法名(参数) [throws 异常类型]) ``` 例如,要匹配 `com.itheima.service.impl.DeptServiceImpl` 类中的 `delete(Integer)` 方法,可以编写如下的切入点表达式[^4]: ```java @Before("execution(void com.itheima.service.impl.DeptServiceImpl.delete(java.lang.Integer))") public void beforeDeleteMethod() { System.out.println("Before delete method is called."); } ``` #### within 表达式 `within` 表达式用来指定包路径下所有的类或某个具体的类内的所有方法作为连接点。 ```java // 所有位于 com.example..* 的类里的任何公共方法调用都会触发此通知 @Pointcut("within(com.example..*)") private void anyPublicOperation() {} ``` #### this 和 target 表达式 - `this`: 当前AOP代理对象类型的执行方法会被匹配到。 - `target`: 被代理的目标对象的类型被匹配。 需要注意的是,在这两种情况下所使用的都是完整的类型名称,并且不允许使用通配符[^5]. ```java // 只要是实现了 AccountService 接口的对象都符合条件 @Around("this(com.example.AccountService)") public Object aroundAccountServiceMethods(ProceedingJoinPoint pjp) throws Throwable { // ... } // 如果目标对象是 CustomerManager 实例,则会应用该增强处理逻辑 @Before("target(org.springframework.samples.service.CustomerManager)") public void auditCustomerChanges() { /* ... */ } ``` #### args 和 @args 表达式 这两个表达式分别基于运行时传递给方法的实际参数以及这些参数所属实例上的注解来进行匹配。 ```java // 对于接受 String 参数的所有方法生效 @AfterReturning(pointcut="args(name)", returning="retVal") public void logReturn(String name, Object retVal){ logger.info("Returning from a method with argument '{}' and value '{}'",name , retVal); } // 针对那些接收带有 @Auditable 注解参数的方法起作用 @Around("@args(com.myapp.security.Auditable)") public Object checkPermissions(ProceedingJoinPoint jp) throws Throwable{ // 权限检查... } ``` #### @Target 和 @Within 表达式 它们允许根据类级别或者方法级别的自定义元数据来定位切面的位置。 ```java // 应用于标注了 Transactional 注解的服务层组件上 @Aspect @Component public class ServiceLayerSecurity { @Before("@annotation(org.springframework.transaction.annotation.Transactional)") public void requireRoleAdminOrSuperUser(){ SecurityContext context = SecurityContextHolder.getContext(); Authentication auth = context.getAuthentication(); if (!(auth != null && (auth.hasAuthority("ROLE_ADMIN") || auth.hasAuthority("SUPER_USER")))){ throw new AccessDeniedException("Insufficient authority"); } } } ``` 通过上述几种常见的切入点表达式的组合运用,能够实现灵活多样的面向切面编程需求。当然除了这里提到的内容之外,还有更多高级特性和配置选项可供探索。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值