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

歡迎訪問 生活随笔!

生活随笔

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

asp.net

Spring 中基于 AOP 的 XML操作方式

發(fā)布時間:2023/12/19 asp.net 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring 中基于 AOP 的 XML操作方式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Spring 框架的一個關鍵組件是面向方面的編程(AOP)框架。面向方面的編程需要把程序邏輯分解成不同的部分稱為所謂的關注點。跨一個應用程序的多個點的功能被稱為橫切關注點,這些橫切關注點在概念上獨立于應用程序的業(yè)務邏輯。有各種各樣的常見的很好的方面的例子,如日志記錄、審計、聲明式事務、安全性和緩存等。

一些AOP術語:

(1) aspect:一個模塊具有一組提供橫切需求的 APIs。例如,一個日志模塊為了記錄日志將被 AOP 方面調(diào)用。應用程序可以擁有任意數(shù)量的方面,這取決于需求。
(2) joint point:相當于ABAP類里可以被增強的method
(3) advice:這是實際行動之前或之后執(zhí)行的方法。這是在程序執(zhí)行期間通過 Spring AOP 框架實際被調(diào)用的代碼。相當于ABAP的pre/post/overwrite exit.
(4) weaving: Weaving 把方面連接到其它的應用程序類型或者對象上,并創(chuàng)建一個被通知的對象。這些可以在編譯時,類加載時和運行時完成。

為了使用 aop 命名空間標簽,需要導入 spring-aop j架構,如下所述:

Beans.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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "><!-- bean definition & AOP specific configuration --></beans>

看個具體的例子:

(1) 新建一個Logging.java:

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()); } }

Beans.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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "><aop:config><aop:aspect id="log" ref="logging"><aop:pointcut id="selectAll" expression="execution(* com.tutorialspoint.*.*(..))"/><aop:before pointcut-ref="selectAll" method="beforeAdvice"/><aop:after pointcut-ref="selectAll" method="afterAdvice"/><aop:after-returning pointcut-ref="selectAll" returning="retVal"method="afterReturningAdvice"/><aop:after-throwing pointcut-ref="selectAll" throwing="ex"method="AfterThrowingAdvice"/></aop:aspect></aop:config><!-- Definition for student bean --><bean id="student" class="com.sap.Student"><property name="name" value="Zara" /><property name="age" value="11"/> </bean><!-- Definition for logging aspect --><bean id="logging" class="com.sap.Logging"/> </beans>

MainApp.java:

import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {public static void main(String[] args) {try {ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");Student student = (Student) context.getBean("student");student.getName();student.getAge(); student.printThrowException();}catch (Exception e) {System.out.println("Jerry ends here!");}} }

輸出:

Jul 26, 2020 11:13:45 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5b80350b: startup date [Sun Jul 26 11:13:45 CST 2020]; root of context hierarchy Jul 26, 2020 11:13:45 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [Beans.xml] Going to setup student profile. Name : Zara Student profile has been setup. Returning:Zara Going to setup student profile. Age : 11 Student profile has been setup. Returning:11 Going to setup student profile. Exception raised Student profile has been setup. There has been an exception: java.lang.IllegalArgumentException Jerry ends here!

要獲取更多Jerry的原創(chuàng)文章,請關注公眾號"汪子熙":

總結

以上是生活随笔為你收集整理的Spring 中基于 AOP 的 XML操作方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。