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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springaop----springaop的使用(一)

發布時間:2023/12/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springaop----springaop的使用(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  與大多數技術一樣, AOP 已經形成了自己的術語。描述切面的常用術語有通知(advice)、切點(pointcut)和連接點(join point)。從今天開始,我們對spring的切面編程做一個總結。她們都是你漫漫長路上,只配錯過的好姑娘。

?

spring中的aop

spring的切面可以應用以下的5種類型的通知:

  • 前置通知(Before):在目標方法被調用之前調用通知功能;
  • 后置通知(After):在目標方法完成之后調用通知,此時不會關心方法的輸出是什么;
  • 返回通知(After-returning):在目標方法成功執行之后調用通知;
  • 異常通知(After-throwing):在目標方法拋出異常后調用通知;
  • 環繞通知(Around):通知包裹了被通知的方法,在被通知的方法調用之前和調用之后執行自定義的行為。

對于spring中aop的支持,主要分為@AspectJ 注解驅動的切面和基于xml的spring的配置。以下我們對這兩種配置做一個簡單的了解。測試的代碼目錄如下:

以后可能對于spring的關于aop學習,這個目錄可能會添加代碼。application-xml.xml是學習xml配置的,application-aop.xml是學習@Aspectj注解配置的。

?

springaop的xml配置方式

?一、我們的application-xml.xml文件內容如下:

<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/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="myService" class="com.linux.huhx.business.service.MyAspectService3"/><bean id="myAspect" class="com.linux.huhx.aspect.XmlUserfulAspect"/><aop:config><aop:aspect id="xmlAspect1" ref="myAspect"><aop:pointcut id="businessService" expression="execution(* com.linux.huhx.business.service.MyAspectService3.*())"/><aop:before method="beforeExecute" pointcut-ref="businessService"/><aop:after method="afterExecute" pointcut-ref="businessService"/></aop:aspect></aop:config> </beans>

?

?二、我們的切面類XmlUserfulAspect如下:

package com.linux.huhx.aspect;/*** @Author: huhx* @Date: 2017-12-15 下午 12:31* @Desc: 切面類*/ public class XmlUserfulAspect {public void beforeExecute() {System.out.println("before execute.");}public void afterExecute() {System.out.println("after execute.");} }

?

三、我們簡單的定義一個應用切面的服務類

package com.linux.huhx.business.service;/*** @Author: huhx* @Date: 2017-12-15 下午 12:28*/ public class MyAspectService3 {public void serviceMethod() {System.out.println("my aspect service1 method.");} }

?

四、在main的類中我們對上述的切面進行測試

package com.linux.huhx.main;import com.linux.huhx.business.service.MyAspectService3; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @Author: huhx* @Date: 2017-12-15 下午 12:32* @Desc: 基于xml配置的aop測試主體類*/ public class XmlServiceMain {private ApplicationContext context = null;@Beforepublic void initApplicationContext() {context = new ClassPathXmlApplicationContext("application-xml.xml");}@Testpublic void aspectServiceTest_1() {MyAspectService3 myAspectService1 = context.getBean("myService", MyAspectService3.class);myAspectService1.serviceMethod();} }

打印的結果如下:

before execute. my aspect service1 method. after execute.

?

springaop中關于注解的方式

  這里面設計的代碼比較多,主要是為了測試不同的知識點。這里全部貼出代碼,后續再做整理。后續我們對這兩種的配置方式做一個比較細致的過程講解。使用aspectj,我們需要在pom.xml文件中添加依賴:

<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.11</version> </dependency>

一、application-aop.xml的文件內容如下

<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/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><aop:aspectj-autoproxy/><bean id="myService1" class="com.linux.huhx.business.service.MyAspectService1"/><bean id="myService2" class="com.linux.huhx.business.service.MyAspectService2"/><!--declare an aspect--><bean id="myAspect" class="com.linux.huhx.aspect.NotVeryUsefulAspect"/><bean id="myAspect1" class="com.linux.huhx.aspect.LittleUserfulAspect"/> </beans>

?

二、兩個測試切面的服務類如下:

  • MyAspectService1:這里面主要測試切面的5種類型的通知。
package com.linux.huhx.business.service;import java.io.FileNotFoundException;/*** @Author: huhx* @Date: 2017-12-15 上午 10:08* @Desc: 一個簡單的測試aspect的實類*/ public class MyAspectService1 {public void serviceMethod() {System.out.println("myaspect service1 method.");}public String returnServiceMethod() {return "return huhx.";}public String throwExceptionMethod() {System.out.println("throw exception method.");try {throw new FileNotFoundException("not found class.");} catch (FileNotFoundException e) {e.printStackTrace();}return "hello world";}public String aroundServiceMethod1() {System.out.println("around service method.");return "huhx";} }
  • MyAspectService2.java:這里面測試切面傳遞參數。
package com.linux.huhx.business.service;import java.util.Map;/*** @Author: huhx* @Date: 2017-12-15 上午 11:17*/ public class MyAspectService2 {public void serviceMethod_1(Map<String, String> map) {System.out.println("service method." + map);} }

?

三、兩個切面的類如下

  • NotVeryUsefulAspect:是針對于上述的MyAspectService1類的,為了測試5種通知類型。
package com.linux.huhx.aspect;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*;/*** @Author: huhx* @Date: 2017-12-15 上午 9:51* @Desc: 定義一個aspect*/@Aspect public class NotVeryUsefulAspect {private static final String pointCutString = "execution(* com.linux.huhx.business.service.MyAspectService1.*())";@Before(pointCutString)public void beforeExecute() {System.out.println("before advice.");}@After(pointCutString)public void afterExecute() {System.out.println("after advice.");}@AfterReturning(value = pointCutString, returning = "retVal")public void afterReturingExecute1(String retVal) {System.out.println("after throwing " + retVal);}@AfterThrowing(value = pointCutString, throwing = "exception")public void afterThrowingExecute1(Throwable exception) {System.out.println("throwing in advice show message: " + exception.getMessage());}@Around(value = pointCutString)public void arundExecute1(ProceedingJoinPoint pjp) throws Throwable{System.out.println("before around.");System.out.println(pjp.proceed());System.out.println("after around.");} }
  • LittleUserfulAspect:是對應于MyAspectService2類的,為了測試切面中參數的傳遞。
package com.linux.huhx.aspect;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut;import java.util.Map;/*** @Author: huhx* @Date: 2017-12-15 上午 11:16* @Desc: 定義一個切面*/ @Aspect public class LittleUserfulAspect {@Pointcut("execution(* com.linux.huhx.business.service.MyAspectService2.*(..)) && args(map,..)")public void anyMethod(Map<String, String> map) {}@Before(value = "anyMethod(map)")public void beforeExecute(Map map) {System.out.println("before execute." + map);} }

?

四、我們的測試主體類ServiceMain

package com.linux.huhx.main;import com.linux.huhx.business.service.MyAspectService1; import com.linux.huhx.business.service.MyAspectService2; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.HashMap; import java.util.Map;/*** @Author: huhx* @Date: 2017-12-15 上午 10:13* @Desc: 測試的main類*/ public class ServiceMain {private ApplicationContext context = null;@Beforepublic void initApplicationContext() {context = new ClassPathXmlApplicationContext("application-aop.xml");}@Testpublic void aspectServiceTest_1() {MyAspectService1 myAspectService1 = context.getBean("myService1", MyAspectService1.class);myAspectService1.serviceMethod();}@Testpublic void aspectServiceTest_2() {MyAspectService1 myAspectService1 = context.getBean("myService1", MyAspectService1.class);myAspectService1.returnServiceMethod();}@Testpublic void aspectServiceTest_3() {MyAspectService1 myAspectService1 = context.getBean("myService1", MyAspectService1.class);myAspectService1.throwExceptionMethod();}@Testpublic void aspectServiceTest_4() {MyAspectService1 myAspectService1 = context.getBean("myService1", MyAspectService1.class);myAspectService1.aroundServiceMethod1();}/* 其它的切面實現類 */@Testpublic void aspectServiceTest_5() {MyAspectService2 myAspectService2 = context.getBean("myService2", MyAspectService2.class);Map<String, String> dataMap = new HashMap<>();dataMap.put("username", "linux");dataMap.put("password", "12345");myAspectService2.serviceMethod_1(dataMap);} }

?

友情鏈接

?

轉載于:https://www.cnblogs.com/huhx/p/baseusespringaop1.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的springaop----springaop的使用(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。