大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 Spring框架中的面向切面編程和AOP

Spring框架中的面向切面編程和AOP

更新時間:2022-11-01 09:35:00 來源:動力節(jié)點 瀏覽1683次

顧名思義,面向切面編程(AOP)在編程中使用方面。它可以定義為將代碼分解為不同的模塊,也稱為模塊化,其中方面是模塊化的關(guān)鍵單元。方面支持橫切關(guān)注點的實現(xiàn),例如事務(wù)、日志記錄,這些不是業(yè)務(wù)邏輯的核心,而不會將代碼核心與其功能混為一談。它通過添加作為現(xiàn)有代碼建議的附加行為來實現(xiàn)。例如,安全性是一個橫切關(guān)注點,在應(yīng)用程序中的許多方法中都可以應(yīng)用安全規(guī)則,因此在每個方法中重復(fù)代碼,在公共類中定義功能,并控制在整個應(yīng)用程序中應(yīng)用該功能。

AOP 中的主導(dǎo)框架:

AOP包括支持和實現(xiàn)代碼模塊化的編程方法和框架。讓我們看一下AOP 中的三個主要框架:

AOP 中的常用術(shù)語:

方面:實現(xiàn) JEE 應(yīng)用程序橫切關(guān)注點(事務(wù)、記錄器等)的類稱為方面。它可以是通過 XML 配置或通過使用 @Aspect 注釋的常規(guī)類配置的普通類。

編織:將方面與建議對象聯(lián)系起來的過程。它可以在加載時、編譯時或運(yùn)行時完成。Spring AOP 在運(yùn)行時進(jìn)行編織。

讓我們編寫我們的第一個方面類,但在此之前看看所需的 jars 和 AOP 的 Bean 配置文件。

package com.aspect
	import org.aspectj.lang.annotation.Aspect;
import Java.lang.RuntimeException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
// Logging class is anotated with @Aspect
// and will contain advices.
@Aspect
class Logging {
}
// The class ImplementAspect
// contains method Aspectcall
// and the advices will be applied
// on that method for demo.
public class ImplementAspect {
	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("my first aspect");
		// **Add beanconfiguration file
		// in your programme when executing.**
		ApplicationContext ctx
			= new ClassPathXmlApplicationContext("beanconfigfile.XML");
		ImplementAspect call
			= (ImplementAspect)ctx.getbean("aspect");
		System.out.println("enter an integer");
		int a = sc.nextInt();
		if (a == 1) {
			throw new RuntimeException(msg);
		}
		else {
			call.aspectCall();
		}
		call.myMethod();
	}
	public void aspectCall()
	{
		System.out.println("Applying advices"
						+ " for the first time");
	}
	public void myMethod()
	{
		System.out.println("This is an"
						+ " extra method");
	}
}

建議:本應(yīng)由 Aspect 完成的工作,或者可以定義為 Aspect 在特定點采取的行動。Advice 有五種類型,即:Before、After、Around、AfterThrowing 和 AfterReturning。讓我們對所有五種類型進(jìn)行簡要討論。

建議類型:

之前:在調(diào)用建議的方法之前運(yùn)行。它由@Before注釋表示。

After:無論結(jié)果如何,無論成功與否,都在建議的方法完成后運(yùn)行。它由@After注釋表示。

AfterReturning:在建議的方法成功完成后運(yùn)行,即沒有任何運(yùn)行時異常。它由@AfterReturning注釋表示。

Around:這是所有建議中最強(qiáng)的建議,??因為它環(huán)繞并在建議方法之前和之后運(yùn)行。這種類型的建議用于我們需要頻繁訪問方法或數(shù)據(jù)庫(如緩存)的地方。它由@Around注釋表示。

AfterThrowing:在建議的方法引發(fā)運(yùn)行時異常后運(yùn)行。它由@AfterThrowing注解表示。

讓我們在 Aspect 類 Logger 中實現(xiàn)所有 5 條建議

// Program to show types of Advices
@Aspect
class Logging {
	// Implementing all the five pieces of advice
	// to execute AfterThrowing advice enter integer value as 1.
	// **Before**
	@Before("execution(public void com.aspect.ImplementAspect.aspectCall())")
	public void loggingAdvice1()
	{
		System.out.println("Before advice is executed");
	}
	// **After**
	@After("execution(public void com.aspect.ImplementAspect.aspectCall())")
	public void loggingAdvice2()
	{
		System.out.println("Running After Advice.");
	}
	// **Around**
	@Around("execution(public void com.aspect.ImplementAspect.myMethod())")
	public void loggingAdvice3()
	{
		System.out.println("Before and After invoking method myMethod");
	}
	// **AfterThrowing**
	@AfterThrowing("execution(" public void com.aspect.ImplementAspect.aspectCall())
	")
		public void
		loggingAdvice4()
	{
		System.out.println("Exception thrown in method");
	}
	// **AfterRunning**
	@AfterReturning("execution(public void com.aspect.ImplementAspect.myMethod())")
	public void loggingAdvice5()
	{
		System.out.println("AfterReturning advice is run");
	}
}

JoinPoints:一個應(yīng)用程序有數(shù)以千計的機(jī)會或點來應(yīng)用 Advice。這些點稱為連接點。例如,可以在每次調(diào)用方法或拋出異常時或在其他各個點應(yīng)用建議。但是 Spring AOP 目前只支持方法執(zhí)行連接點(建議在 Spring bean 上執(zhí)行方法)。

讓我們看看連接點在我們的@Aspect 類(Logger)中做了什么

// Program to show JoinPoints
@Aspect
class Logging {
	// Passing a JoinPoint Object
	// into parameters of the method
	// with the annotated advice
	// enables to print the information
	/// when the advice is executed
	// with the help of toString() method
	// present in it.
	@Before("execution(public void com.aspect.ImplementAspect.aspectCall())")
	public void loggingAdvice1(JoinPoint joinpoint)
	{
		System.out.println("Before advice is executed");
		System.out.println(joinpoint.toString());
	}
}

切入點:由于在代碼的每個點都應(yīng)用建議是不可行的,因此,最終應(yīng)用建議的選定連接點稱為切入點。通常,您使用顯式的類和方法名稱或通過定義匹配的類和方法名稱模式的正則表達(dá)式來指定這些切入點。它有助于通過編寫一次并在多個點使用來減少重復(fù)代碼,讓我們看看如何。

// Program to shgw PointCuts
@Aspect
class Logging {
	// pointcut() is a dummy method
	// required to hold @Pointcut annotation
	// pointcut() can be used instead of writing line 1
	// whenever required, as done in line 4.
	// This prevents a repetition of code.
	@Pointcut("execution(public void com.aspect.ImplementAspect.aspectCall())") // line 1
	public void pointCut()
	{
	}
	// pointcut() is used to avoid repetition of code
	@Before("pointcut()")
	public void loggingAdvice1()
	{
		System.out.println("Before advice is executed");
	}
}

以上就是關(guān)于“Spring框架中的面向切面編程和AOP”的介紹,大家如果對此比較感興趣,想了解更多相關(guān)知識,不妨來關(guān)注一下本站的Spring教程,里面還有更豐富的知識等著大家去學(xué)習(xí),相信對大家一定會有所幫助的。

提交申請后,顧問老師會電話與您溝通安排學(xué)習(xí)

免費課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 99这里只有| 午夜深夜福利网址 | 久久久久欧美激情 | 中文国产成人精品久久一 | 久久精品国产亚洲麻豆小说 | 中文在线免费不卡视频 | 小视频国产 | 亚洲香蕉在线 | 综合久久久久综合体桃花网 | 老司机午夜永久在线观看 | 欧美亚洲国产成人综合在线 | 天天射久久| 国产精品线在线精品国语 | 爱爱小视频成人免费 | 久热这里只精品99re8久 | 国产欧美一区二区精品性色99 | 亚洲欧美日韩成人网 | 99久久中文字幕 | 日本不卡一 | 国产欧美一区二区三区沐欲 | 日韩欧美印度一级毛片 | 图片亚洲va欧美va国产综合 | 日本美女视频韩国视频网站免费 | 天天干夜夜曰 | 99色在线视频 | 爱爱小视频成人免费 | 奇米影视盒7777| 97免费观看 | 老色99久久九九精品尤物 | 精品国产一区二区三区四区色 | 欧美毛片aaaaa片久久久久 | 在线不卡福利 | 欧美级毛片 | 国产区久久 | 四虎高清成人永久免费影院 | 久久国产视屏 | 日韩免费看 | 黄色片在线免费观看视频 | 99热这里只有精品首页 | 国产高清区 | 毛片一级在线观看 |