SpringAOP中pointcutexpression表达式解析及匹配多个条件

更新时间:2023-07-04 14:01:12 阅读: 评论:0

SpringAOP中pointcutexpression表达式解析及匹配多个条件
Pointcut 是指那些⽅法需要被执⾏"AOP",是由"Pointcut Expression"来描述的.
Pointcut可以有下列⽅式来定义或者通过&& || 和!的⽅式进⾏组合.
小程序是什么args()
@args()
execution()
this()
target()
@target()
within()
@within()
@annotation
其中 execution 是⽤的最多的,其格式为:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
returning type pattern,name pattern, and parameters pattern是必须的.
ret-type-pattern:可以为*表⽰任何返回值,全路径的类名等.
name-pattern:指定⽅法名,*代表所以,t*,代表以t开头的所有⽅法.
parameters pattern:指定⽅法参数(声明的类型),(..)代表所有参数,(*)代表⼀个参数,(*,String)代表第⼀个参数为任何值,第⼆个为String类型.举例说明:
任意公共⽅法的执⾏:
execution(public * *(..))
任何⼀个以“t”开始的⽅法的执⾏:
execution(* t*(..))
AccountService 接⼝的任意⽅法的执⾏:
execution(* vice.AccountService.*(..))
定义在rvice包⾥的任意⽅法的执⾏:
execution(* vice.*.*(..))
定义在rvice包和所有⼦包⾥的任意类的任意⽅法的执⾏:
execution(* vice..*.*(..))
定义在pointcutexp包和所有⼦包⾥的JoinPointObjP2类的任意⽅法的执⾏:
execution(* st.spring.aop.pointcutexp..JoinPointObjP2.*(..))")
***> 最靠近(..)的为⽅法名,靠近.*(..))的为类名或者接⼝名,如上例的JoinPointObjP2.*(..))
<aop:config>
<aop:pointcut expression="execution(* avelskyboy.dao..*.find*(..)) ||
execution(* avelskyboy.dao..*.query*(..))"
id="findCachePointcut" />
<aop:advisor advice-ref="jdbcInterceptor" pointcut-ref="findCachePointcut" />
</aop:config>
在多个表达式之间使⽤  || , or 表⽰或,使⽤  && , and 表⽰与,!表⽰
.
上⾯的代码也可以改写成
<aop:config>
<aop:pointcut expression=" ( execution(* avelskyboy.dao..*.find*(..)))  or(execution(* avelskyboy.dao..*.query*(..))
)
"
id="findCachePointcut" />
<aop:advisor advice-ref="jdbcInterceptor" pointcut-ref="findCachePointcut" />
</aop:config>
注意上⾯两中⽅法的不同点出了将 || 改成了 or ,还有就是每个execution都被()包含起来,建议为了区分不同的表达式最好都是⽤()包装。
pointcutexp包⾥的任意类.
st.spring.aop.pointcutexp.*)
pointcutexp包和所有⼦包⾥的任意类.
st.spring.aop.pointcutexp..*)
实现了Intf接⼝的所有类,如果Intf不是接⼝,限定Intf单个类.
st.spring.aop.pointcutexp.Intf)
***> 当⼀个实现了接⼝的类被AOP的时候,⽤getBean⽅法必须cast为接⼝类型,不能为该类的类型.
带有@Transactional标注的所有类的任意⽅法.
@within(ansaction.annotation.Transactional)
@target(ansaction.annotation.Transactional)
带有@Transactional标注的任意⽅法.
@annotation(ansaction.annotation.Transactional) ***> @within和@target针对类的注解,@annotation是针对⽅法的注解
参数带有@Transactional标注的⽅法.
@args(ansaction.annotation.Transactional)
芹菜叶子可以吃吗参数为String类型(运⾏是决定)的⽅法.
args(String)
Pointcut 可以通过Java注解和XML两种⽅式配置,如下所⽰:
<bean id="bravemandao" class="st.dao.BraveManDao">
<property name="barveman" ref="braveman"></property>
</bean>
<bean id="braveman" class="st.bean.BraveMan">
</bean>
<bean id="minstrel" class="st.bean.Minstrel"></bean>
<aop:config>
<aop:aspect ref="minstrel">
<aop:pointcut expression="execution(* *.test(..))" id="say"/>
励志小说<aop:before pointcut-ref="say" method="singBeforeSay"/>
<aop:after pointcut-ref="say" method="singAfterSay"/>
</aop:aspect>
</aop:config>
st.bean;
public class Minstrel {
public void singBeforeSay(){
System.out.println("before say");
}
public void singAfterSay(){植树节活动主题
System.out.println("after say");
}
胃痛吃什么}
public class BraveManDao {
笔记本无线BraveMan braveman;
public BraveMan getBarveman() {
回宝珍饺子return braveman;
drag是什么意思}
public void tBarveman(BraveMan braveman) {
this.braveman = braveman;
}
public void test(){
braveman.say();
}
}

本文发布于:2023-07-04 14:01:12,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1078003.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:参数   定义   路径
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图