八、Spring AOPはxmlのいくつかの通知の種類に基づいて構成されています.(事前通知、後置通知、異常通知、最終通知)
12554 ワード
章のspringAOP入門事例に続き、前の章のコードに基づいて修正を行います.まず、工具類にもう四つの通知タイプを加えます.
package com.lp.utils;
/**
* ,
*
* @Date 2020/5/29 14:41
* @Author luopeng
*/
public class Logger {
/**
*
*/
public void beforePrintLog(){
System.out.println(" Logger printLog 。。。");
}
/**
*
*/
public void afterReturningPrintLog(){
System.out.println(" Logger printLog 。。。");
}
/**
*
*/
public void afterThrowingPrintLog(){
System.out.println(" Logger printLog 。。。");
}
/**
*
*/
public void afterPrintLog(){
System.out.println(" Logger printLog 。。。");
}
}
その後、私たちはxmlの設定ファイルを修正します.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="accountService" class="com.lp.service.impl.AccountServiceImpl"/>
<bean id="logger" class="com.lp.utils.Logger"/>
<aop:config>
<aop:pointcut id="pt1" expression="execution(* com.lp.service.impl.*.*(..))"/>
<aop:aspect id="logAdvice" ref="logger">
<aop:before method="beforePrintLog"
pointcut-ref="pt1">aop:before>
<aop:after-returning method="afterReturningPrintLog"
pointcut-ref="pt1">aop:after-returning>
<aop:after-throwing method="afterThrowingPrintLog"
pointcut-ref="pt1">aop:after-throwing>
<aop:after method="afterPrintLog"
pointcut-ref="pt1">aop:after>
aop:aspect>
aop:config>
beans>
上記の構成ファイルでは、私たちは
を利用して、切り込み点表現を構成しています.このように、4つの通知タイプは私たちが設定した切り込み点表現式のIDを直接引用すればいいです.この構成は通知タイプの前に配置し、外部にできるだけ配置し、グローバルに利用可能です.重複コードが減少しました.