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

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

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

更新時(shí)間:2022-11-01 09:35:00 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1934次

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

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

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

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

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

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

讓我們編寫(xiě)我們的第一個(gè)方面類(lèi),但在此之前看看所需的 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 在特定點(diǎn)采取的行動(dòng)。Advice 有五種類(lèi)型,即:Before、After、Around、AfterThrowing 和 AfterReturning。讓我們對(duì)所有五種類(lèi)型進(jìn)行簡(jiǎn)要討論。

建議類(lèi)型:

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

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

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

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

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

讓我們?cè)?Aspect 類(lèi) Logger 中實(shí)現(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:一個(gè)應(yīng)用程序有數(shù)以千計(jì)的機(jī)會(huì)或點(diǎn)來(lái)應(yīng)用 Advice。這些點(diǎn)稱(chēng)為連接點(diǎn)。例如,可以在每次調(diào)用方法或拋出異常時(shí)或在其他各個(gè)點(diǎn)應(yīng)用建議。但是 Spring AOP 目前只支持方法執(zhí)行連接點(diǎn)(建議在 Spring bean 上執(zhí)行方法)。

讓我們看看連接點(diǎn)在我們的@Aspect 類(lèi)(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());
	}
}

切入點(diǎn):由于在代碼的每個(gè)點(diǎn)都應(yīng)用建議是不可行的,因此,最終應(yīng)用建議的選定連接點(diǎn)稱(chēng)為切入點(diǎn)。通常,您使用顯式的類(lèi)和方法名稱(chēng)或通過(guò)定義匹配的類(lèi)和方法名稱(chēng)模式的正則表達(dá)式來(lái)指定這些切入點(diǎn)。它有助于通過(guò)編寫(xiě)一次并在多個(gè)點(diǎn)使用來(lái)減少重復(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”的介紹,大家如果對(duì)此比較感興趣,想了解更多相關(guān)知識(shí),不妨來(lái)關(guān)注一下本站的Spring教程,里面還有更豐富的知識(shí)等著大家去學(xué)習(xí),相信對(duì)大家一定會(huì)有所幫助的。

提交申請(qǐng)后,顧問(wèn)老師會(huì)電話(huà)與您溝通安排學(xué)習(xí)

  • 全國(guó)校區(qū) 2025-06-26 搶座中
免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 美女羞羞视频 | 波多野结衣久久一区二区 | 日韩 视频在线播放 | 特级aav毛片日本免费视频 | 亚洲精品色一区色二区色三区 | 九九综合视频 | 欧美交换乱理伦片120秒 | 欧美大屁股精品毛片视频 | 欧美日本另类xxx乱大交 | 亚洲精品久久久久久久久久ty | 图片亚洲va欧美va国产综合 | 日本久久中文字幕 | 免费一级特黄欧美大片久久网 | 99热免费观看 | 中国在线播放精品区 | 暗香影院午夜国产精品 | 亚洲欧美精品天堂久久综合一区 | 国产精品一区二区手机看片 | 国产精品美女一级在线观看 | 欧美一区二区在线观看视频 | 99热久久这里只有精品6国产网 | 欧美日本三级 | 中文字幕1区2区 | 久久99国产亚洲高清观看首页 | 久久精品国产清白在天天线 | 国内精品久久久久久西瓜色吧 | 中文字幕在线观看一区 | 久久久99精品 | 99在线热视频只有精品免费 | 四虎影视亚洲精品 | 伊人影院中文字幕 | 精品国产免费观看一区 | 国产精品一级毛片不收费 | 国产精品视频免费 | 四虎色姝姝影院www 四虎色影院 | 国产毛片一区二区三区精品 | 久久精品国产99国产精品亚洲 | 91亚洲免费 | 亚洲图片综合区 | 久久精品综合视频 | 国产欧美日韩综合二区三区 |