Spring--AOP面向切面程式設計,Java資料結構面試題及答案
阿新 • • 發佈:2021-09-20
專案結構:
在pom.xml中匯入相關依賴:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency>
新建業務層介面和實現類:
/** * @Author: Ly * @Date: 2020-08-02 22:14 */ public interface IAccountService { /** * 模擬儲存賬戶 */ void saveAccount(); /** * 模擬修改賬戶 * @param i */ void updateAccount(int i); /** * 模擬刪除賬戶 * @return */ int deleteAccount(); } /** * @Author: Ly * @Date: 2020-08-02 22:17 */ public class AccountServiceImpl implements IAccountService { public void saveAccount() { System.out.println("執行了儲存"); } public void updateAccount(int i) { System.out.println("執行力修改"); } public int deleteAccount() { System.out.println("執行了刪除"); return 0; } }
新建通知類:
/** * @Author: Ly * @Date: 2020-08-02 22:19 */ public class Logger { /** * 前置通知 */ public void beforePrintLog(){ System.out.println("前置Logger類中的printLog方法開始記錄日誌了"); } /** * 後置通知 */ public void afterReturningPrintLog(){ System.out.println("後置Logger類中的printLog方法開始記錄日誌了"); } /** * 異常通知 */ public void afterThrowPrintLog(){ System.out.println("異常Logger類中的printLog方法開始記錄日誌了"); } /** * 最終通知 */ public void afterPrintLog(){ System.out.println("最終Logger類中的printLog方法開始記錄日誌了"); } public Object aroundPrintLog(ProceedingJoinPoint pjp){ Object rtValue = null; try { Object[] args=pjp.getArgs();//得到方法執行所需的引數 System.out.println("前Logger類中的aroundPrintLog方法開始記錄日誌了"); rtValue=pjp.proceed(args);//明確呼叫業務層方法(切入點方法) System.out.println("後Logger類中的aroundPrintLog方法開始記錄日誌了"); return rtValue; } catch (Throwable e) { System.out.println("異常Logger類中的aroundPrintLog方法開始記錄日誌了"); throw new RuntimeException(e); }finally { System.out.println("最終Logger類中的aroundPrintLog方法開始記錄日誌了"); } } }
配置bean.xml檔案:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--配置spring的Ioc,把service物件配置進來-->
<bean id="accountService" class="com.ly.service.impl.AccountServiceImpl"></bean>
<!--配置Logger類-->
<bean id="logger" class="com.ly.utils.Logger"></bean>
<!--配置AOP-->
<aop:config>
<!--只能在配置切面上方-->
<aop:pointcut id="pt2" expression="execution(public void com.ly.service.impl.*.*(..))"/>
<!--配置切面-->
<aop:aspect id="logAdvice" ref="logger">
<!--配置前置通知:在切入點方法執行之前執行-->
<aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>
<!--配置後置通知:在切入點方法正常執行之後執行-->
<aop:after-returning method="afterReturningPrintLog" pointcut="execution(public void com.ly.service.impl.*.*(..))"></aop:after-returning>
<!--配置異常通知:在切入點方法執行產生異常之後執行-->
<aop:after-throwing method="afterThrowPrintLog" pointcut="execution(public void com.ly.service.impl.*.*(..))"></aop:after-throwing>
<!--最終通知:無論切入點方法是否正常執行都會在其後面執行-->
<aop:after method="afterPrintLog" pointcut="execution(public void com.ly.service.impl.*.*(..))"></aop:after>
## 最後
這次要給大家分享總結的東西就是這些了
**[CodeChina開源專案:【一線大廠Java面試題解析+核心總結學習筆記+最新講解視訊】](https://ali1024.coding.net/public/P7/Java/git)**
最後再分享一份終極手撕架構的大禮包(學習筆記):分散式+微服務+開源框架+效能優化
![image](https://upload-images.jianshu.io/upload_images/24613101-d8272b45047eaee2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)