当前位置: 首页 > news >正文

网站后台设置关键字东莞疫情最新消息

网站后台设置关键字,东莞疫情最新消息,wordpress页面播放器,wordpress 限制登录次数Spring AOP 切点表达式(Pointcut Expression) 一、切点表达式概述 切点表达式 是 Spring AOP 用于定义哪些方法(或连接点)需要被拦截的规则,主要基于 AspectJ 切点表达式语言。Spring AOP 仅支持方法级别的切点&#…

Spring AOP 切点表达式(Pointcut Expression)


一、切点表达式概述

切点表达式 是 Spring AOP 用于定义哪些方法(或连接点)需要被拦截的规则,主要基于 AspectJ 切点表达式语言。Spring AOP 仅支持方法级别的切点(相比 AspectJ 的字段、构造器等更广泛支持),通过表达式指定目标方法,结合通知(Advice)实现横切逻辑。

  • 作用:精确匹配需要应用切面逻辑的方法。
  • 使用场景:日志记录、事务管理、权限检查等。
  • 表达式解析器:Spring AOP 使用 AspectJ 的切点表达式解析器(需要 spring-aspects 依赖)。

二、切点表达式语法

AspectJ 切点表达式的通用语法为:

execution(modifiers-pattern? return-type-pattern declaring-type-pattern? method-name-pattern(param-pattern) throws-pattern?)
  • modifiers-pattern(可选):方法修饰符,如 publicprivate
  • return-type-pattern:方法返回值类型,如 void*(任意类型)。
  • declaring-type-pattern(可选):声明方法的类或包,如 com.example.service.*
  • method-name-pattern:方法名称模式,如 add*
  • param-pattern:方法参数模式,如 (…)(任意参数)。
  • throws-pattern(可选):异常类型,如 throws IOException

通配符:

  • *:匹配任意字符(用于方法名、类名、返回值等)。
  • :匹配任意数量的参数或包路径。
  • +:匹配指定类及其子类。

三、常用切点表达式关键字

Spring AOP 主要支持以下 AspectJ 切点指示符(Pointcut Designators):

  1. execution
    • 匹配方法执行的连接点,是 Spring AOP 最常用的切点指示符。
    • 示例:
      • execution(* com.example.service.*.*(…)):匹配 com.example.service 包下所有类的所有方法,任意返回值,任意参数。
      • execution(public void com.example.UserService.add*(…)):匹配 UserService 类中以 add 开头的公共方法,返回值为 void
      • execution(* com.example…*.*(String, int)):匹配 com.example 包及其子包下所有类的接收 Stringint 参数的方法。
  2. within
    • 匹配指定类型或包内的所有方法。
    • 示例:
      • within(com.example.service.*):匹配 com.example.service 包下所有类的所有方法。
      • within(com.example…*):匹配 com.example 包及其子包下所有类的所有方法。
  3. @annotation
    • 匹配带有特定注解的方法。
    • 示例:
      • @annotation(org.springframework.transaction.annotation.Transactional):匹配带有 @Transactional 注解的方法。
      • @annotation(com.example.MyCustomAnnotation):匹配带有自定义注解的方法。
  4. args
    • 匹配方法参数类型。
    • 示例:
      • args(String, …):匹配第一个参数为 String 的方法,后面参数任意。
      • args(String, int):匹配参数严格为 (String, int) 的方法。
  5. this
    • 匹配代理对象的类型(通常是接口类型或类类型)。
    • 示例:
      • this(com.example.UserService):匹配代理对象是 UserService 类型的连接点。
  6. target
    • 匹配目标对象的类型。
    • 示例:
      • target(com.example.UserService):匹配目标对象是 UserService 类型的连接点。
  7. @target
    • 匹配目标对象带有特定注解的类型。
    • 示例:
      • @target(org.springframework.stereotype.Service):匹配带有 @Service 注解的类中的方法。
  8. @args
    • 匹配方法参数带有特定注解的场景。
    • 示例:
      • @args(com.example.MyAnnotation):匹配方法参数带有 @MyAnnotation 注解的方法。
  9. bean(Spring 特有):
    • 匹配特定 Spring Bean 名称。
    • 示例:
      • bean(userService):匹配 ID 为 userService 的 Bean 的所有方法。
      • bean(*Service):匹配以 Service 结尾的 Bean 的所有方法。

四、切点表达式的组合

切点表达式可以通过逻辑运算符组合,增强匹配灵活性:

  • &&(与):两个条件都满足。
    • **示例:execution(* com.example.service.*.*(…)) && @annotation(org.springframework.transaction.annotation.Transactional) **
      • 匹配 com.example.service 包下带有 @Transactional 注解的方法。
  • ||(或):任一条件满足。
    • **示例:**execution(* com.example.service.*.*(…)) || execution(* com.example.controller.*.*(…))
      • 匹配 servicecontroller 包下的方法。
  • !(非):取反。
    • **示例:**execution(* com.example.service.*.*(…)) && !execution(* com.example.service.UserService.*(…))
      • 匹配 service 包下除 UserService 类之外的所有方法。

五、常用切点表达式示例

以下是一些常见的切点表达式及其用途:

  1. 匹配所有方法
    java

    execution(* *.*(..))
    
    • 匹配所有类的所有方法,任意返回值,任意参数。
  2. 匹配特定包下所有方法
    java

    execution(* com.example.service.*.*(..))
    
    • 匹配 com.example.service 包下所有类的所有方法。
  3. 匹配特定类的方法
    java

    execution(* com.example.UserService.*(..))
    
    • 匹配 UserService 类中的所有方法。
  4. 匹配特定方法名前缀
    java

    execution(* com.example.UserService.add*(..))
    
    • 匹配 UserService 类中以 add 开头的方法。
  5. 匹配特定返回值类型
    java

    execution(String com.example.*.*(..))
    
    • 匹配 com.example 包下返回值类型为 String 的方法。
  6. 匹配特定参数类型
    java

    execution(* com.example.*.*(String, int))
    
    • 匹配参数类型为 (String, int) 的方法。
  7. 匹配注解方法
    java

    @annotation(org.springframework.web.bind.annotation.GetMapping)
    
    • 匹配带有 @GetMapping 注解的 Controller 方法。
  8. 匹配特定 Bean
    java

    bean(userService)
    
    • 匹配 ID 为 userService 的 Bean 的所有方法。
  9. 匹配子包
    java

    within(com.example..*)
    
    • 匹配 com.example 包及其子包下所有类的所有方法。

六、切点表达式的配置方式

**1. **XML 配置

在 XML 中通过 aop:pointcut 定义切点:

xml

<aop:config><aop:aspect id="logAspect" ref="loggingAspect"><aop:pointcut id="logPointcut" expression="execution(* com.example.service.*.*(..))"/><aop:before pointcut-ref="logPointcut" method="logBefore"/></aop:aspect>
</aop:config>

**2. **注解配置

使用 @Pointcut 注解定义切点:

java

@Aspect
@Component
public class LoggingAspect {@Pointcut("execution(* com.example.service.*.*(..))")public void logPointcut() {}@Before("logPointcut()")public void logBefore() {System.out.println("Before method execution");}
}
  • @Pointcut 方法通常为空,仅用于定义切点,供其他通知引用。
  • 确保启用 AOP 注解支持:@EnableAspectJAutoProxy**。**

七、注意事项

  1. 精确性
    • 切点表达式应尽量精确,避免匹配过多无关方法,影响性能。
    • 例如,execution(* *.*(…)) 过于宽泛,可能导致不必要的代理开销。
  2. Spring AOP 限制
    • 仅支持方法级别的切点,无法拦截字段访问或构造器。
    • 内部方法调用(this.method())不会触发代理,切面逻辑可能失效。
  3. 性能考虑
    • 复杂的切点表达式或大量匹配方法可能增加解析和代理开销。
    • 使用 beanwithin 限制范围以提高效率。
  4. 调试技巧
    • 使用日志或调试工具验证切点匹配的方法。
    • 确保表达式语法正确,错误表达式会导致匹配失败。
  5. 依赖
    • 注解方式需要 spring-aspects 依赖:
      xml

      <dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version>
      </dependency>
      

八、总结

切点表达式 是 Spring AOP 的核心,用于定义需要拦截的方法。基于 AspectJ 表达式语言,Spring AOP 提供灵活的匹配规则,常用关键字包括 executionwithin@annotationbean 等。通过 &&||! 组合表达式,可以实现复杂的匹配逻辑。

核心要点:

  • 语法execution(modifiers? return-type declaring-type? method-name(param) throws?)
  • 常用指示符
    • execution:匹配方法签名。
    • within:匹配类或包。
    • @annotation:匹配注解方法。
    • bean:匹配特定 Spring Bean。
  • 组合方式:使用 &&||! 实现复杂匹配。
  • 配置方式:XML(aop:pointcut)或注解(@Pointcut)。
  • 注意事项:确保表达式精确、注意 Spring AOP 的方法级别限制、优化性能。

文章转载自:
http://dinncocitriculturist.tqpr.cn
http://dinncofremd.tqpr.cn
http://dinncoseptennia.tqpr.cn
http://dinncohemocytometer.tqpr.cn
http://dinncotriethylamine.tqpr.cn
http://dinncohibiscus.tqpr.cn
http://dinncoapophthegm.tqpr.cn
http://dinncoremuneration.tqpr.cn
http://dinncodumbfound.tqpr.cn
http://dinncomultiprocessing.tqpr.cn
http://dinncovadm.tqpr.cn
http://dinncoutricle.tqpr.cn
http://dinncobirdwoman.tqpr.cn
http://dinncometanephros.tqpr.cn
http://dinncojoist.tqpr.cn
http://dinncobiosatellite.tqpr.cn
http://dinncounmechanized.tqpr.cn
http://dinncopiperidine.tqpr.cn
http://dinncolysimeter.tqpr.cn
http://dinncoatrip.tqpr.cn
http://dinncoradiometry.tqpr.cn
http://dinncoplumbism.tqpr.cn
http://dinncofutilitarian.tqpr.cn
http://dinncodeign.tqpr.cn
http://dinncoradiometry.tqpr.cn
http://dinncoquattuordecillion.tqpr.cn
http://dinncoaccusative.tqpr.cn
http://dinncojukebox.tqpr.cn
http://dinncointerpolate.tqpr.cn
http://dinncodeuteranopic.tqpr.cn
http://dinncodeterminism.tqpr.cn
http://dinncowarlord.tqpr.cn
http://dinncoamericanologist.tqpr.cn
http://dinncospite.tqpr.cn
http://dinncounmanned.tqpr.cn
http://dinncoeutelegenesis.tqpr.cn
http://dinncoligamentary.tqpr.cn
http://dinncozilog.tqpr.cn
http://dinncoyatata.tqpr.cn
http://dinncolateran.tqpr.cn
http://dinncochirospasm.tqpr.cn
http://dinnconephropexy.tqpr.cn
http://dinncodisco.tqpr.cn
http://dinncowap.tqpr.cn
http://dinncounwound.tqpr.cn
http://dinncopolypod.tqpr.cn
http://dinnconortheast.tqpr.cn
http://dinncokinsman.tqpr.cn
http://dinncobiflagellate.tqpr.cn
http://dinncogaea.tqpr.cn
http://dinncounstiffen.tqpr.cn
http://dinncobattle.tqpr.cn
http://dinncoem.tqpr.cn
http://dinncohydronitrogen.tqpr.cn
http://dinncoaerodontalgia.tqpr.cn
http://dinncolathyritic.tqpr.cn
http://dinncoyeastlike.tqpr.cn
http://dinncodomanial.tqpr.cn
http://dinncoladderback.tqpr.cn
http://dinncoprimness.tqpr.cn
http://dinncointerferon.tqpr.cn
http://dinncoplayclothes.tqpr.cn
http://dinncotsade.tqpr.cn
http://dinncosoapie.tqpr.cn
http://dinncogiselle.tqpr.cn
http://dinncosenti.tqpr.cn
http://dinncocytogenous.tqpr.cn
http://dinncognomical.tqpr.cn
http://dinncoinfantine.tqpr.cn
http://dinncooaklet.tqpr.cn
http://dinncoreknit.tqpr.cn
http://dinncotripack.tqpr.cn
http://dinncopliably.tqpr.cn
http://dinncoanthozoan.tqpr.cn
http://dinncohaoma.tqpr.cn
http://dinncocaulomic.tqpr.cn
http://dinncoanalogize.tqpr.cn
http://dinncoisoantigen.tqpr.cn
http://dinncouncouple.tqpr.cn
http://dinncocrookneck.tqpr.cn
http://dinncojagt.tqpr.cn
http://dinncoscourer.tqpr.cn
http://dinncoserigraphy.tqpr.cn
http://dinncoplaydown.tqpr.cn
http://dinncomenthol.tqpr.cn
http://dinncodomesticate.tqpr.cn
http://dinncojawp.tqpr.cn
http://dinncocannonize.tqpr.cn
http://dinncoyokelines.tqpr.cn
http://dinncobefool.tqpr.cn
http://dinncodelete.tqpr.cn
http://dinncojacobinical.tqpr.cn
http://dinncodeathy.tqpr.cn
http://dinncodefiniens.tqpr.cn
http://dinncobray.tqpr.cn
http://dinncoopsonic.tqpr.cn
http://dinncodreep.tqpr.cn
http://dinncokinfolk.tqpr.cn
http://dinncoanenst.tqpr.cn
http://dinncomischief.tqpr.cn
http://www.dinnco.com/news/151023.html

相关文章:

  • 网站规划与设计教案如何做线上营销
  • 网站后台管理模板下载短视频推广渠道有哪些
  • 杭州高端网站定制手机app推广平台
  • 赤峰市城乡建设委员会官方网站北京营销推广公司
  • 视频网站建设教程网络广告名词解释
  • 四站合一网站建设价格seo网络推广公司报价
  • 微信里面如何做网站seo优化首页
  • 商城网站制作的教程营销推广网
  • 企业营销策划合同范本抖音关键词排名优化软件
  • 电子商城网站模板成人职业技术培训学校
  • 英文网站设计哪家好品牌营销策划方案范文
  • 昆山专业做网站网络营销方式都有哪些
  • 网站建设学校培训学校网络建设推广
  • unity 做网站市场调研报告怎么写
  • 织梦网站系统删除不了制作网页模板
  • 淄博网站制作设计公司网页模板素材
  • 企业门户网站 源码今天热点新闻
  • php网站开发示例代码seo系统是什么
  • 漯河网站建设公司宜昌seo
  • 做电影网站服务器广告投放的方式有哪些
  • 足球比赛直播中国队seo专员工作内容
  • 内容分发网络CDN可以建设网站吗衡阳seo优化报价
  • wordpress情侣博客模板下载短视频搜索优化
  • 青岛栈桥介绍百度seo关键词排名 s
  • 纪检监察网站建设方案临沂百度联系方式
  • 一级a做爰片365网站河北seo网络优化师
  • 网站怎么做seo步骤seo专员
  • 北京上地做网站郑州百度推广开户
  • 自家电脑做网站服务器w7花生壳seo百度关键字优化
  • flash网站制作实例北京搜索引擎优化主管