日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

spring基于XML的AOP-编写必要的代码

發布時間:2024/4/13 asp.net 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring基于XML的AOP-编写必要的代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?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.learn</groupId><artifactId>day03_learn_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> package com.learn.utils;/*** 用于記錄日志的工具類,它里面提供了公共的代碼*/ public class Logger {/*** 用于打印日志:計劃讓其在切入點方法執行之前執行(切入點方法就是業務層方法)*/public void printLog(){System.out.println("Logger類中的pringLog方法開始記錄日志了。。。");} } package com.learn.service;/*** 賬戶的業務層接口*/ public interface IAccountService {/*** 模擬保存賬戶*/void saveAccount();/*** 模擬更新賬戶* @param i*/void updateAccount(int i);/*** 刪除賬戶* @return*/int deleteAccount(); } package com.learn.service.impl;import com.learn.service.IAccountService;/*** 賬戶的業務層實現類*/ public class AccountServiceImpl implements IAccountService{public void saveAccount() {System.out.println("執行了保存");}public void updateAccount(int i) {System.out.println("執行了更新"+i);}public int deleteAccount() {System.out.println("執行了刪除");return 0;} } <?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.learn.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.learn.service.impl.AccountServiceImpl.saveAccount()訪問修飾符可以省略void com.learn.service.impl.AccountServiceImpl.saveAccount()返回值可以使用通配符,表示任意返回值* com.learn.service.impl.AccountServiceImpl.saveAccount()包名可以使用通配符,表示任意包。但是有幾級包,就需要寫幾個*.* *.*.*.*.AccountServiceImpl.saveAccount())包名可以使用..表示當前包及其子包* *..AccountServiceImpl.saveAccount()類名和方法名都可以使用*來實現通配* *..*.*()參數列表:可以直接寫數據類型:基本類型直接寫名稱 int引用類型寫包名.類名的方式 java.lang.String可以使用通配符表示任意類型,但是必須有參數可以使用..表示有無參數均可,有參數可以是任意類型全通配寫法:* *..*.*(..)實際開發中切入點表達式的通常寫法:切到業務層實現類下的所有方法* com.learn.service.impl.*.*(..)--><!-- 配置Logger類 --><bean id="logger" class="com.learn.utils.Logger"></bean><!--配置AOP--><aop:config><!--配置切面 --><aop:aspect id="logAdvice" ref="logger"><!-- 配置通知的類型,并且建立通知方法和切入點方法的關聯--><aop:before method="printLog" pointcut="execution(* com.learn.service.impl.*.*(..))"></aop:before></aop:aspect></aop:config></beans> package com.learn.test;import com.learn.service.IAccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 測試AOP的配置*/ public class AOPTest {public static void main(String[] args) {//1.獲取容器ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.獲取對象IAccountService as = (IAccountService)ac.getBean("accountService");//3.執行方法as.saveAccount();as.updateAccount(1);as.deleteAccount();} }

?

總結

以上是生活随笔為你收集整理的spring基于XML的AOP-编写必要的代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。