springaop----springaop的使用(一)
生活随笔
收集整理的這篇文章主要介紹了
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種類型的通知。
- MyAspectService2.java:這里面測試切面傳遞參數。
?
三、兩個切面的類如下
- NotVeryUsefulAspect:是針對于上述的MyAspectService1類的,為了測試5種通知類型。
- LittleUserfulAspect:是對應于MyAspectService2類的,為了測試切面中參數的傳遞。
?
四、我們的測試主體類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的使用(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在mybatis中resultMap与r
- 下一篇: 主题与颜色--Dcat-Admin框架实