Spring支持 @AspectJ注释样式方法和基于模式的方法来实现自定义方面.
基于XML架构
使用常规类和基于XML的配置实现方面.
要使用本节中描述的AOP命名空间标记,您需要导入spring AOP模式,描述如下 :
<?xml version = "1.0" encoding = "UTF-8"?><beans xmlns = "https://img01.yuandaxia.cn/Content/img/tutorials/springaop/" xmlns:xsi = "https://img01.yuandaxia.cn/Content/img/tutorials/springaop/XMLSchema-instance" xmlns:aop = "https://img01.yuandaxia.cn/Content/img/tutorials/springaop/" xsi:schemaLocation = "http://www.springframework.org/schema/beans https://img01.yuandaxia.cn/Content/img/tutorials/springaop/spring-beans-3.0.xsd http://www.springframework.org/schema/aop https://img01.yuandaxia.cn/Content/img/tutorials/springaop/spring-aop-3.0.xsd "> <!-- bean definition & AOP specific configuration --></beans>
声明一个方面
方面是使用< aop:aspect> 元素,使用 ref 属性引用支持bean,如下所示.
<aop:config> <aop:aspect id = "myAspect" ref = "aBean"> ... </aop:aspect></aop:config><bean id = "aBean" class = "..."> ...</bean>
这里将配置"aBean"并像其他任何Spring bean一样注入依赖项,如前几章所述.
声明PointCut
A PointCut 有助于确定要使用不同建议执行的感兴趣的连接点(即方法).在使用基于XML Schema的配置时,PointCut将定义如下 :
<aop:config> <aop:aspect id = "myAspect" ref = "aBean"> <aop:PointCut id = "businessService" expression = "execution(* com.xyz.myapp.service.*.*(..))"/> ... </aop:aspect></aop:config><bean id = "aBean" class = "..."> ...</bean>
以下示例定义了一个名为"businessService"的PointCut,它将匹配com.it1352包下Student类中可用的getName()方法的执行.
<aop:config> <aop:aspect id = "myAspect" ref = "aBean"> <aop:PointCut id = "businessService" expression = "execution(* com.IT屋.Student.getName(..))"/> ... </aop:aspect></aop:config><bean id = "aBean" class = "..."> ...</bean>
声明建议
您可以在< aop:aspect>中声明五个建议中的任何一个.使用< aop:{ADVICE NAME}>元素如下.
<aop:config> <aop:aspect id = "myAspect" ref = "aBean"> <aop:PointCut id = "businessService" expression = "execution(* com.xyz.myapp.service.*.*(..))"/> <!-- a before advice definition --> <aop:before PointCut-ref = "businessService" method = "doRequiredTask"/> <!-- an after advice definition --> <aop:after PointCut-ref = "businessService" method = "doRequiredTask"/> <!-- an after-returning advice definition --> <!--The doRequiredTask method must have parameter named retVal --> <aop:after-returning PointCut-ref = "businessService" returning = "retVal" method = "doRequiredTask"/> <!-- an after-throwing advice definition --> <!--The doRequiredTask method must have parameter named ex --> <aop:after-throwing PointCut-ref = "businessService" throwing = "ex" method = "doRequiredTask"/> <!-- an around advice definition --> <aop:around PointCut-ref = "businessService" method = "doRequiredTask"/> ... </aop:aspect></aop:config><bean id = "aBean" class = "..."> ...</bean>
您可以对不同的建议使用相同的 doRequiredTask 或不同的方法.这些方法将被定义为方面模块的一部分.
@AspectJ基于
@AspectJ是指将方面声明为常规Java类的样式用Java 5注释注释. @AspectJ指的是将方面声明为使用Java 5注释注释的常规Java类的样式.通过在基于XML Schema的配置文件中包含以下元素来启用@AspectJ支持.
<aop:aspectj-autoproxy/>
声明一个Aspect
Aspects类与任何其他普通bean一样,可能有任何其他方法和字段class,除了它们将使用@Aspect注释如下.
package org.xyz;import org.aspectj.lang.annotation.Aspect;@Aspectpublic class AspectModule {}
它们将被配置为XML与任何其他bean一样如下.
<bean id = "myAspect" class = "org.xyz.AspectModule"> <!-- configure properties of aspect here as normal --></bean>
声明PointCut
A PointCut 有助于确定连接点(即方法) )有兴趣用不同的建议执行.在使用基于@AspectJ的配置时,PointCut声明有两个部分 :
一个PointCut表达式,它确切地确定哪些方法执行我们感兴趣的是.
包含名称和任意数量参数的PointCut签名.该方法的实际主体是无关紧要的,实际上应该是空的.
以下示例定义了一个名为'businessService'的PointCut匹配com.xyz.myapp.service包下的类中可用的每个方法的执行.
import org.aspectj.lang.annotation.PointCut;@PointCut("execution(* com.xyz.myapp.service.*.*(..))") // expression private void businessService() {} // signature
以下示例定义一个名为'getname'的PointCut,它将匹配com.it1352包下Student类中可用的getName()方法的执行.
import org.aspectj.lang.annotation.PointCut;@PointCut("execution(* com.it1352.Student.getName(..))") private void getname() {}
声明建议
您可以使用下面给出的@ {ADVICE-NAME}注释声明五个建议中的任何一个.这假定您已经定义了一个PointCut签名方法businessService().
@Before("businessService()")public void doBeforeTask(){ ...}@After("businessService()")public void doAfterTask(){ ...}@AfterReturning(PointCut = "businessService()", returning = "retVal")public void doAfterReturnningTask(Object retVal){ // you can intercept retVal here. ...}@AfterThrowing(PointCut = "businessService()", throwing = "ex")public void doAfterThrowingTask(Exception ex){ // you can intercept thrown exception here. ...}@Around("businessService()")public void doAroundTask(){ ...}
您可以为任何建议定义PointCut内联.以下是为建议之前定义内联PointCut的示例.
@Before("execution(* com.xyz.myapp.service.*.*(..))")public doBeforeTask(){ ...}
免责声明:以上内容(如有图片或视频亦包括在内)有转载其他网站资源,如有侵权请联系删除
-
TV动画《入间同学入魔了》第三季制作确认 将于10月播出
根据西修原作制作的TV动画《入间同学入魔了》第三季宣布了将于2022年10月播出的消息。本作的宣传图也一并公开。在这次的新宣传图上,可以看到主人公入间和新角色巴巴托斯的样子。 《入间同学入魔了》作品讲述了被父母卖给了恶魔的14岁少年铃木入间在魔界学园生活的故事。TV动画第一季和第二季分别在20
-
华硕Z690主板发布 12代酷睿致胜神装
-
S9:ISGvsSPY 欧洲蛇咬下大白鲨赢得小组第一名!
{@SYS-PAGE-SPLIT@}
-
韩国画师冷门动物拟人作品
-
《幻塔》开服福利少?别被骗了 游戏里藏着大量白给的好东西
-
克苏鲁+伊藤润二《恐怖世界》2023年夏季推出1.0版 并登陆主机
-
iPhone15ProMax预计售价2万块,网曝有这些改变