springaop----springaop的使用(一)
與大多數(shù)技術(shù)一樣, AOP 已經(jīng)形成了自己的術(shù)語。描述切面的常用術(shù)語有通知(advice)、切點(pointcut)和連接點(join point)。從今天開始,我們對spring的切面編程做一個總結(jié)。她們都是你漫漫長路上,只配錯過的好姑娘。
?
spring中的aop
spring的切面可以應(yīng)用以下的5種類型的通知:
- 前置通知(Before):在目標(biāo)方法被調(diào)用之前調(diào)用通知功能;
- 后置通知(After):在目標(biāo)方法完成之后調(diào)用通知,此時不會關(guān)心方法的輸出是什么;
- 返回通知(After-returning):在目標(biāo)方法成功執(zhí)行之后調(diào)用通知;
- 異常通知(After-throwing):在目標(biāo)方法拋出異常后調(diào)用通知;
- 環(huán)繞通知(Around):通知包裹了被通知的方法,在被通知的方法調(diào)用之前和調(diào)用之后執(zhí)行自定義的行為。
對于spring中aop的支持,主要分為@AspectJ 注解驅(qū)動的切面和基于xml的spring的配置。以下我們對這兩種配置做一個簡單的了解。測試的代碼目錄如下:
以后可能對于spring的關(guān)于aop學(xué)習(xí),這個目錄可能會添加代碼。application-xml.xml是學(xué)習(xí)xml配置的,application-aop.xml是學(xué)習(xí)@Aspectj注解配置的。
?
springaop的xml配置方式
?一、我們的application-xml.xml文件內(nèi)容如下:
<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.");} }?
三、我們簡單的定義一個應(yīng)用切面的服務(wù)類
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的類中我們對上述的切面進(jìn)行測試
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();} }打印的結(jié)果如下:
before execute. my aspect service1 method. after execute.?
springaop中關(guān)于注解的方式
這里面設(shè)計的代碼比較多,主要是為了測試不同的知識點。這里全部貼出代碼,后續(xù)再做整理。后續(xù)我們對這兩種的配置方式做一個比較細(xì)致的過程講解。使用aspectj,我們需要在pom.xml文件中添加依賴:
<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.11</version> </dependency>一、application-aop.xml的文件內(nèi)容如下
<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>?
二、兩個測試切面的服務(wù)類如下:
- MyAspectService1:這里面主要測試切面的5種類型的通知。
- MyAspectService2.java:這里面測試切面?zhèn)鬟f參數(shù)。
?
三、兩個切面的類如下
- NotVeryUsefulAspect:是針對于上述的MyAspectService1類的,為了測試5種通知類型。
- LittleUserfulAspect:是對應(yīng)于MyAspectService2類的,為了測試切面中參數(shù)的傳遞。
?
四、我們的測試主體類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();}/* 其它的切面實現(xiàn)類 */@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);} }?
友情鏈接
?
轉(zhuǎn)載于:https://www.cnblogs.com/huhx/p/baseusespringaop1.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的springaop----springaop的使用(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在mybatis中resultMap与r
- 下一篇: 主题与颜色--Dcat-Admin框架实