1.我對Aop的了解是在不改變源代碼的基礎上,擴充新的功能。
2.概念解釋:
public class User {
/**
* 連接配接點:類裡面可以被增強的方法
* 切入點:類裡面已經被增強的方法
* 通知/增強:擴充的功能(邏輯)
* ①前置通知:在方法之前執行
* ②後置通知:在方法之後執行(出現異常也會執行)
* ③異常通知:方法出現異常時候執行
* ④傳回通知:在後置之前執行(出現異常不會執行,方法成功傳回後執行)
* ⑤環繞通知:在方法之前和之後執行(包住方法)
* 切面:把增強用到方法的過程叫增強(注意是一個過程)
*/
public void show(){//這個show方法叫 連接配接點
System.out.print("我是AopUser");
}
}
3.spring使用aspectj來做aop
①aspectj不是spring的一部分
②spring2.0新增了對aspectj的支援
4.實作aop有2種方式
①xml方式
②注解方式
5.導入jar包
在導入spring核心包的基礎上,還需要導入aop相關的jar包
spring-aspects-5.0.5.RELEASE.jar
spring-aop-5.0.5.RELEASE.jar
aopalliance.jar
aspectjweaver-1.8.7.jar
6.常用表達式
execution(<通路修飾符>?<傳回類型><方法名>(<參數>)<異常>)
(1) execution(* test3.AopUser.show1(..))
(2) execution( test3.AopUser.(..))
(3) execution( .*(..))
(4)execution( show(..))比對所有show開頭的方法
7.xml方式
①建立User類
@Component
public void show(){
//int i=1/0;異常通知的時候用
System.out.println("我是User的show方法");
}
②建立增強類:
public class AopMethod {
public void before(){
System.out.println("我是前置通知");
}
public void after(){
System.out.println("我是後置通知");
}
public void last(){
System.out.println("我是最終通知");
}
public void exception(){
System.out.println("我是異常通知");
}
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("環繞通知開始");
proceedingJoinPoint.proceed();
System.out.println("環繞通知結束");
}
③applicationContext.xml檔案
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="test3"></context:component-scan>
<bean id="AopMethod" class="test3.AopMethod"></bean>
<aop:config>
<aop:pointcut id="userShow" expression="execution(* test3.User.show(..))"></aop:pointcut>
<aop:aspect ref="AopMethod">
<!--<aop:before method="before" pointcut-ref="userShow"></aop:before>-->
<!--<aop:after method="after" pointcut-ref="userShow"></aop:after>-->
<!--<aop:around method="around" pointcut-ref="userShow"></aop:around>-->
<!-- <aop:after-throwing method="exception" pointcut-ref="userShow"></aop:after-throwing>-->
<!--<aop:after-returning method="last" pointcut-ref="userShow"></aop:after-returning>-->
</aop:aspect>
</aop:config>
④測試類MainTest
public class MainTest {
public static void main(String[] args){
//1.加載xml檔案
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.拿到user對象,這裡user是bean裡面的id
User user = (User)context.getBean("user");
user.show();
}
⑤運作結果...
8.注解方式
@Before前置通知
@AfterReturning傳回通知
@Around環繞通知
@AfterThrowing異常通知
@After(後置通知)最終通知(最終final通知,不管是否異常,該通知都會執行)
①在applicationContext.xml中加入
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
applicationContext.xml檔案:
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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--開啟aop操作-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
②User類:
public void show(){
//int i=1/0;//異常通知的時候用
System.out.println("我是User的show方法");
}
③AopMethod類:
@Aspect
@Before(value = "execution(* test3.User.show(..))")
public void before(){
System.out.println("我是前置通知");
}
@AfterReturning(value = "execution(* test3.User.show(..))")
public void after(){
System.out.println("我是傳回通知");
}
@After(value = "execution(* test3.User.show(..))")
public void last(){
System.out.println("我是後置通知");
}
@AfterThrowing(value = "execution(* test3.User.show(..))")
public void exception(){
System.out.println("我是異常通知");
}
@Around(value = "execution(* test3.User.show(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("環繞通知開始");
proceedingJoinPoint.proceed();
System.out.println("環繞通知結束");
}
④MainTest類:
public static void main(String[] args){
//1.加載xml檔案
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.拿到user對象,這裡user是bean裡面的id
User user = (User)context.getBean("user");
user.show();
}
⑤運作結果: