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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

SpringAop两种配置:xml配置和注解方式

發(fā)布時(shí)間:2024/3/13 javascript 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringAop两种配置:xml配置和注解方式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一,什么是SpringAop?

? ? ? ? 所謂的springaop就是面向切面編程,就是在你的原有項(xiàng)目功能上,通過aop去添加新的功能,這些功能是建立在原有的功能基礎(chǔ)上的,而且不會(huì)修改原來(lái)的動(dòng)能代碼以及功能邏輯。例如你用銀行卡購(gòu)物,購(gòu)物付款,這是一個(gè)功能。付款后,銀行向你的手機(jī)發(fā)送一條取錢信息,這就是新加的功能。也就是實(shí)現(xiàn)了增強(qiáng)

二,Springaop的實(shí)現(xiàn)機(jī)制是什么?

? ? ? ? 橫向抽取機(jī)制,那么什么是橫向抽取機(jī)制呢?所謂的橫向抽取機(jī)制就是使用動(dòng)態(tài)的代理的方式(cglib代理和jdk代理)來(lái)實(shí)現(xiàn)對(duì)象的代理,實(shí)際上我們操作的是假對(duì)象。既然有橫向抽取機(jī)制,那么有沒有縱向代理模式呢 ?答案是有的。那么什么是縱向抽取呢?縱向抽取就是把公共的方法寫在父類里,所有的類都繼承父類,這樣就是能調(diào)用父類的方法。例如,你購(gòu)物付款是一個(gè)子類的功能,你可能還會(huì)取款,這也是一個(gè)功能,而在他們結(jié)束之后,銀行都會(huì)發(fā)送一個(gè)信息給你,這又是一個(gè)功能,這個(gè)銀行給你發(fā)送信息是個(gè)公共的方法,所以這個(gè)發(fā)信息的功能就是屬于父類的。子類繼承父類并調(diào)用父類的方法就是縱向抽取。

三,Springaop的使用場(chǎng)景

? ? 一般來(lái)說:打印日志,還有短信通知啊,權(quán)限驗(yàn)證等,都可以使用aop來(lái)實(shí)現(xiàn)。

四,sringaop的兩種實(shí)現(xiàn)方式

? ? ? ? ? (1)xml文件配置方式

? ? ? ? ? (2)注解的方式實(shí)現(xiàn)

??方式一:xml文件方式配置

?導(dǎo)入AOPjar包

?創(chuàng)建如圖所示項(xiàng)目結(jié)構(gòu)

?

創(chuàng)建核心類的接口

package com.su.service;public interface BookService { // 添加int save(int n); // 刪除int del(); // 修改int update(); // 查詢void find(); }

? 創(chuàng)建核心類(被加強(qiáng)類)

package com.su.service.impl;import com.su.service.BookService;public class BookServiceImpl implements BookService {@Overridepublic int save(int n) {System.out.println("添加");return 1;}@Overridepublic int del() {System.out.println("刪除");return 1;}@Overridepublic int update() {System.out.println("修改");return 1;}@Overridepublic void find() {System.out.println("查詢");} }

?創(chuàng)建增強(qiáng)類

package com.su.advice;import org.aspectj.lang.ProceedingJoinPoint;public class Loger {public void check(){System.out.println("前置通知/增強(qiáng):執(zhí)行系統(tǒng)的權(quán)限驗(yàn)證");}public void logPrint(){System.out.println("后置通知/增強(qiáng):執(zhí)行日志的打印");}public void exception(){System.out.println("異常通知/增強(qiáng):做出異常處理");}public void distory(){System.out.println("最終通知/增強(qiáng):資源釋放");}public Object around(ProceedingJoinPoint pjp) {try {//前置增強(qiáng)System.out.println("環(huán)繞通知---前置增強(qiáng)");//通過ProceedingJoinPoint 完成代理對(duì)象的方法調(diào)用Object result = null;//定義返回值變量Object[] args = pjp.getArgs();//獲取參數(shù)列表result = pjp.proceed(args);//后置增強(qiáng)System.out.println("環(huán)繞通知---后置增強(qiáng)");return result;} catch (Throwable e) {//異常通知System.out.println("環(huán)繞通知----異常增強(qiáng)");throw new RuntimeException(e);} finally {//最終增強(qiáng)System.out.println("環(huán)繞通知----最終增強(qiáng)");}} }

?配置切點(diǎn)和切面

<?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:context="http://www.springframework.org/schema/context"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/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--1.把所有類的對(duì)象交給IOC容器進(jìn)行管理--><bean id="loger" class="com.su.advice.Loger"/><bean id="bookService" class="com.su.service.impl.BookServiceImpl"/><!--2.AOP的配置:讓增強(qiáng)類 的 哪個(gè)方法 動(dòng)態(tài)進(jìn)行何種增強(qiáng) 核心類 的 哪個(gè)方法--><aop:config><!--配置 增強(qiáng)類的哪個(gè)方法 對(duì) 核心類的哪個(gè)方法 進(jìn)行 何種增強(qiáng)--><aop:aspect id="log" ref="loger"> <!-- <aop:before method="check" pointcut="execution(* *..BookServiceImpl.*(..))"/>--> <!-- <aop:after-returning method="logPrint" pointcut="execution(* *..BookServiceImpl.*(..))"/>--> <!-- <aop:after-throwing method="exception" pointcut="execution(* *..BookServiceImpl.*(..))"/>--> <!-- <aop:after method="distory" pointcut="execution(* *..BookServiceImpl.*(..))"/>--> <!--環(huán)繞增強(qiáng)--><aop:around method="around" pointcut="execution(* *..BookServiceImpl.*(..))"/></aop:aspect></aop:config></beans>

?創(chuàng)建測(cè)試類

package com.su.servlet;import com.su.service.BookService; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test01 {@Testpublic void test01(){ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");BookService bookService=context.getBean(BookService.class);bookService.save(5);} }

運(yùn)行結(jié)果


?由上面的配置文件可以看出,配置相當(dāng)復(fù)雜,接下來(lái)使用spring注解的方式。


方式二:通過springAop注解實(shí)現(xiàn)

導(dǎo)入AOPjar包

創(chuàng)建如圖所示項(xiàng)目結(jié)構(gòu)

創(chuàng)建核心類的接口

package com.su.service;public interface BookService { // 添加int save(int n); // 刪除int del(); // 修改int update(); // 查詢void find(); }

創(chuàng)建核心類(被加強(qiáng)類)

package com.su.service.impl;import com.su.service.BookService;public class BookServiceImpl implements BookService {@Overridepublic int save(int n) {System.out.println("添加");return 1;}@Overridepublic int del() {System.out.println("刪除");return 1;}@Overridepublic int update() {System.out.println("修改");return 1;}@Overridepublic void find() {System.out.println("查詢");} }

2創(chuàng)建增強(qiáng)類并打上注解

@Aspec:增強(qiáng)

@Component:定義Spring管理Bean(也就是將標(biāo)注@Component注解的類交由spring管理)

@Before:前置通知
@AfterReturning:后置通知
@AfterThrowing:異常通知

@After:最終通知

@Around:環(huán)繞通知

package com.su.advice;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component;/*** 注解*/ @Component //告訴系統(tǒng)loger是增強(qiáng)類 @Aspect public class Loger {//@Before:前置通知@Before("execution(* *..BookServiceImpl.*(..))")public void check(){System.out.println("前置通知/增強(qiáng):執(zhí)行系統(tǒng)的權(quán)限驗(yàn)證");}//@AfterReturning:后置通知@AfterReturning("execution(* *..BookServiceImpl.*(..))")public void logPrint(){ System.out.println("后置通知/增強(qiáng):執(zhí)行日志的打印"); }//@AfterThrowing:異常通知@AfterThrowing("execution(* *..BookServiceImpl.*(..))")public void exception(){ System.out.println("異常通知/增強(qiáng):做出異常處理"); }//@After:最終通知@After("execution(* *..BookServiceImpl.*(..))")public void distory(){ System.out.println("最終通知/增強(qiáng):資源釋放"); }//@Around:環(huán)繞通知@Around("execution(* *..BookServiceImpl.*(..))")public Object around(ProceedingJoinPoint pjp) {try {//前置增強(qiáng)System.out.println("環(huán)繞通知---前置增強(qiáng)");//通過ProceedingJoinPoint 完成代理對(duì)象的方法調(diào)用Object result = null;//定義返回值變量Object[] args = pjp.getArgs();//獲取參數(shù)列表result = pjp.proceed(args);//后置增強(qiáng)System.out.println("環(huán)繞通知---后置增強(qiáng)");return result;} catch (Throwable e) {//異常通知System.out.println("環(huán)繞通知----異常增強(qiáng)");throw new RuntimeException(e);} finally {//最終增強(qiáng)System.out.println("環(huán)繞通知----最終增強(qiáng)");}} }

?配置切點(diǎn)和切面

<?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:context="http://www.springframework.org/schema/context"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/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--1.掃描component及同名注解--><context:component-scan base-package="com.su"/><!--2.開啟AOP注解支持--><aop:aspectj-autoproxy/> </beans>

??創(chuàng)建測(cè)試類

package com.su.servlet;import com.su.service.BookService; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test01 {@Testpublic void test01(){ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");BookService bookService=context.getBean(BookService.class);bookService.save(5);} }

?運(yùn)行結(jié)果

總結(jié)

以上是生活随笔為你收集整理的SpringAop两种配置:xml配置和注解方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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