當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring-学习笔记08【面向切面编程AOP】
生活随笔
收集整理的這篇文章主要介紹了
Spring-学习笔记08【面向切面编程AOP】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- Java后端 學習路線 筆記匯總表【黑馬程序員】
目錄
01 AOP的概念
02 spring中的aop術語和細節
03 spring基于XML的AOP-編寫必要的代碼
03.1、pom.xml
03.2、IAccountService.java
03.3、AccountServiceImpl.java
03.4、Logger.java
03.5、AOPTest.java
04 spring基于XML的AOP-配置步驟
05 切入點表達式的寫法
05.1、bean.xml
06 四種常用通知類型
07 通用化切入點表達式
08 spring中的環繞通知
09 spring基于注解的AOP配置
10 總結和作業安排
01 AOP的概念
02 spring中的aop術語和細節
通知的類型03 spring基于XML的AOP-編寫必要的代碼
03.1、pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.itheima</groupId><artifactId>day03_eesy_03springAOP</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><dependencies><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></dependencies></project>03.2、IAccountService.java
package com.itheima.service;/*** 賬戶的業務層接口*/ public interface IAccountService {/*** 模擬保存賬戶*/void saveAccount();/*** 模擬更新賬戶** @param i*/void updateAccount(int i);/*** 刪除賬戶** @return*/int deleteAccount(); }03.3、AccountServiceImpl.java
package com.itheima.service.impl;import com.itheima.service.IAccountService;/*** 賬戶的業務層實現類*/ public class AccountServiceImpl implements IAccountService {@Overridepublic void saveAccount() {System.out.println("執行了保存.");}@Overridepublic void updateAccount(int i) {System.out.println("執行了更新:" + i);}@Overridepublic int deleteAccount() {System.out.println("執行了刪除.");return 0;} }03.4、Logger.java
package com.itheima.utils;/*** 用于記錄日志的工具類,它里面提供了公共的代碼*/ public class Logger {/*** 用于打印日志:計劃讓其在切入點方法執行之前執行(切入點方法就是業務層方法)*/public void printLog() {System.out.println("Logger類中的pringLog方法開始記錄日志了...");} }03.5、AOPTest.java
04 spring基于XML的AOP-配置步驟
05 切入點表達式的寫法
05.1、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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置srping的Ioc,把service對象配置進來--><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean><!--spring中基于XML的AOP配置步驟1、把通知Bean也交給spring來管理2、使用aop:config標簽表明開始AOP的配置3、使用aop:aspect標簽表明配置切面id屬性:是給切面提供一個唯一標識ref屬性:是指定通知類bean的Id。4、在aop:aspect標簽的內部使用對應標簽來配置通知的類型我們現在示例是讓printLog方法在切入點方法執行之前執行,所以是前置通知aop:before:表示配置前置通知method屬性:用于指定Logger類中哪個方法是前置通知pointcut屬性:用于指定切入點表達式,該表達式的含義指的是對業務層中哪些方法增強切入點表達式的寫法:關鍵字:execution(表達式)表達式:訪問修飾符 返回值 包名.包名.包名...類名.方法名(參數列表)標準的表達式寫法:public void com.itheima.service.impl.AccountServiceImpl.saveAccount()訪問修飾符可以省略void com.itheima.service.impl.AccountServiceImpl.saveAccount()返回值可以使用通配符,表示任意返回值* com.itheima.service.impl.AccountServiceImpl.saveAccount()包名可以使用通配符,表示任意包。但是有幾級包,就需要寫幾個*.* *.*.*.*.AccountServiceImpl.saveAccount())包名可以使用..表示當前包及其子包* *..AccountServiceImpl.saveAccount()類名和方法名都可以使用*來實現通配* *..*.*()參數列表:可以直接寫數據類型:基本類型直接寫名稱 int引用類型寫包名.類名的方式 java.lang.String可以使用通配符表示任意類型,但是必須有參數可以使用..表示有無參數均可,有參數可以是任意類型全通配寫法:* *..*.*(..)實際開發中切入點表達式的通常寫法:切到業務層實現類下的所有方法* com.itheima.service.impl.*.*(..)--><!-- 配置Logger類 --><bean id="logger" class="com.itheima.utils.Logger"></bean><!--配置AOP--><aop:config><!--配置切面 --><aop:aspect id="logAdvice" ref="logger"><!-- 配置通知的類型,并且建立通知方法和切入點方法的關聯--><aop:before method="printLog" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:before></aop:aspect></aop:config></beans>06 四種常用通知類型
07 通用化切入點表達式
08 spring中的環繞通知
09 spring基于注解的AOP配置
?
10 總結和作業安排
通知的類型總結
以上是生活随笔為你收集整理的Spring-学习笔记08【面向切面编程AOP】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring-学习笔记07【银行转账案例
- 下一篇: Spring-学习笔记09【JdbcTe