javascript
SpringAop两种配置:xml配置和注解方式
一,什么是SpringAop?
? ? ? ? 所謂的springaop就是面向切面編程,就是在你的原有項目功能上,通過aop去添加新的功能,這些功能是建立在原有的功能基礎上的,而且不會修改原來的動能代碼以及功能邏輯。例如你用銀行卡購物,購物付款,這是一個功能。付款后,銀行向你的手機發送一條取錢信息,這就是新加的功能。也就是實現了增強
二,Springaop的實現機制是什么?
? ? ? ? 橫向抽取機制,那么什么是橫向抽取機制呢?所謂的橫向抽取機制就是使用動態的代理的方式(cglib代理和jdk代理)來實現對象的代理,實際上我們操作的是假對象。既然有橫向抽取機制,那么有沒有縱向代理模式呢 ?答案是有的。那么什么是縱向抽取呢?縱向抽取就是把公共的方法寫在父類里,所有的類都繼承父類,這樣就是能調用父類的方法。例如,你購物付款是一個子類的功能,你可能還會取款,這也是一個功能,而在他們結束之后,銀行都會發送一個信息給你,這又是一個功能,這個銀行給你發送信息是個公共的方法,所以這個發信息的功能就是屬于父類的。子類繼承父類并調用父類的方法就是縱向抽取。
三,Springaop的使用場景
? ? 一般來說:打印日志,還有短信通知啊,權限驗證等,都可以使用aop來實現。
四,sringaop的兩種實現方式
? ? ? ? ? (1)xml文件配置方式
? ? ? ? ? (2)注解的方式實現
??方式一:xml文件方式配置
?導入AOPjar包
?創建如圖所示項目結構
?
創建核心類的接口
package com.su.service;public interface BookService { // 添加int save(int n); // 刪除int del(); // 修改int update(); // 查詢void find(); }? 創建核心類(被加強類)
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("查詢");} }?創建增強類
package com.su.advice;import org.aspectj.lang.ProceedingJoinPoint;public class Loger {public void check(){System.out.println("前置通知/增強:執行系統的權限驗證");}public void logPrint(){System.out.println("后置通知/增強:執行日志的打印");}public void exception(){System.out.println("異常通知/增強:做出異常處理");}public void distory(){System.out.println("最終通知/增強:資源釋放");}public Object around(ProceedingJoinPoint pjp) {try {//前置增強System.out.println("環繞通知---前置增強");//通過ProceedingJoinPoint 完成代理對象的方法調用Object result = null;//定義返回值變量Object[] args = pjp.getArgs();//獲取參數列表result = pjp.proceed(args);//后置增強System.out.println("環繞通知---后置增強");return result;} catch (Throwable e) {//異常通知System.out.println("環繞通知----異常增強");throw new RuntimeException(e);} finally {//最終增強System.out.println("環繞通知----最終增強");}} }?配置切點和切面
<?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.把所有類的對象交給IOC容器進行管理--><bean id="loger" class="com.su.advice.Loger"/><bean id="bookService" class="com.su.service.impl.BookServiceImpl"/><!--2.AOP的配置:讓增強類 的 哪個方法 動態進行何種增強 核心類 的 哪個方法--><aop:config><!--配置 增強類的哪個方法 對 核心類的哪個方法 進行 何種增強--><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.*(..))"/>--> <!--環繞增強--><aop:around method="around" pointcut="execution(* *..BookServiceImpl.*(..))"/></aop:aspect></aop:config></beans>?創建測試類
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);} }運行結果
?由上面的配置文件可以看出,配置相當復雜,接下來使用spring注解的方式。
方式二:通過springAop注解實現
導入AOPjar包
創建如圖所示項目結構
創建核心類的接口
package com.su.service;public interface BookService { // 添加int save(int n); // 刪除int del(); // 修改int update(); // 查詢void find(); }創建核心類(被加強類)
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創建增強類并打上注解
@Aspec:增強
@Component:定義Spring管理Bean(也就是將標注@Component注解的類交由spring管理)
@Before:前置通知
@AfterReturning:后置通知
@AfterThrowing:異常通知
@After:最終通知
@Around:環繞通知
package com.su.advice;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component;/*** 注解*/ @Component //告訴系統loger是增強類 @Aspect public class Loger {//@Before:前置通知@Before("execution(* *..BookServiceImpl.*(..))")public void check(){System.out.println("前置通知/增強:執行系統的權限驗證");}//@AfterReturning:后置通知@AfterReturning("execution(* *..BookServiceImpl.*(..))")public void logPrint(){ System.out.println("后置通知/增強:執行日志的打印"); }//@AfterThrowing:異常通知@AfterThrowing("execution(* *..BookServiceImpl.*(..))")public void exception(){ System.out.println("異常通知/增強:做出異常處理"); }//@After:最終通知@After("execution(* *..BookServiceImpl.*(..))")public void distory(){ System.out.println("最終通知/增強:資源釋放"); }//@Around:環繞通知@Around("execution(* *..BookServiceImpl.*(..))")public Object around(ProceedingJoinPoint pjp) {try {//前置增強System.out.println("環繞通知---前置增強");//通過ProceedingJoinPoint 完成代理對象的方法調用Object result = null;//定義返回值變量Object[] args = pjp.getArgs();//獲取參數列表result = pjp.proceed(args);//后置增強System.out.println("環繞通知---后置增強");return result;} catch (Throwable e) {//異常通知System.out.println("環繞通知----異常增強");throw new RuntimeException(e);} finally {//最終增強System.out.println("環繞通知----最終增強");}} }?配置切點和切面
<?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>??創建測試類
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);} }?運行結果
總結
以上是生活随笔為你收集整理的SpringAop两种配置:xml配置和注解方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android图片缓存框架 - Fres
- 下一篇: php需要session么,php –