举个例子
一个aspect切面class
public class Logging {
/**
* This is the method which I would like to execute
* before a selected method execution.
*/
public void beforeAdvice() {
System.out.println("Going to setup student profile.");
}
/**
* This is the method which I would like to execute
* after a selected method execution.
*/
public void afterAdvice() {
System.out.println("Student profile has been setup.");
}
/**
* This is the method which I would like to execute
* when any method returns.
*/
public void afterReturningAdvice(Object retVal) {
System.out.println("Returning:" + retVal.toString());
}
/**
* This is the method which I would like to execute
* if there is an exception raised.
*/
public void AfterThrowingAdvice(IllegalArgumentException ex) {
System.out.println("There has been an exception: " + ex.toString());
}
}
SpringAOP.xml配置
<bean id="student" class="com.seeyon.SpringBean.aop.Student" p:name="yangyu" p:age="27"></bean>
<bean id="logging" class="com.seeyon.SpringBean.aop.Logging"></bean>
<aop:config>
<!-- 切面class -->
<aop:aspect id="log" ref="logging">
<!-- 切点 -->
<aop:pointcut id="studentMethod" expression="execution(* com.seeyon.SpringBean.aop.Student.get*(..))"/>
<!-- 方法执行之前触发切面class的beforeAdvice方法 -->
<aop:before pointcut-ref="studentMethod" method="beforeAdvice"/>
<!-- 方法执行之后触发切面class的afterAdvice方法 -->
<aop:after pointcut-ref="studentMethod" method="afterAdvice"/>
</aop:aspect>
</aop:config>
术语说明
术语 | 说明 |
---|
Aspect(切面) | 泛指交叉业务逻辑,比如日志处理、事务处理的逻辑可以理解为切面,上面xml中aop:aspect标签表示的就是一个切面。 |
JoinPoint(连接点) | 连接点指切面可以织入的位置,是一个虚拟的概念,可以理解为所有满足切点扫描条件的所有的时机。 |
Pointcut(切入点) | 切面具体切入的位置,上面aop:pointcut标签表示的就是一个切点,也就是满足条件被扫描到的目标方法。 |
Advice(通知/增强) | 切面的一种实现,可以完成切面的功能的函数,例如日志记录,权限验证,事务控制,性能检测,错误信息检测等。 |
Weaving(织入) | 指将切面的逻辑插入到目标对象中的过程。 |
参考:https://www.jianshu.com/p/c57a5a998c6b
怎么收藏这篇文章?
看的我热血沸腾啊https://www.237fa.com/
不错不错,我喜欢看 https://www.237fa.com/
看的我热血沸腾啊https://www.ea55.com/
看的我热血沸腾啊www.jiwenlaw.com