- aop基本概念
- aop操作準(zhǔn)備
- 使用表達(dá)式配置切入點(diǎn)
aop操作基本概念
1 在spring里面進(jìn)行aop操作网严,使用 aspectj(餓死怕死J)實(shí)現(xiàn)
(1)aspectj不是spring一部分识樱,和spring一起使用進(jìn)行aop操作
(2)Spring2.0以后新增了對AspectJ支持
2 使用aspectj實(shí)現(xiàn)aop有兩種方式
(1)基于aspectj的xml配置
(2)基于aspectj的注解方式
aop操作準(zhǔn)備
導(dǎo)入Maven依賴和aop約束
<!-- ioc編程 -->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
使用表達(dá)式配置切入點(diǎn)
1 切入點(diǎn):實(shí)際增強(qiáng)的方法
2 常用的表達(dá)式
execution(<訪問修飾符>?<返回類型><方法名>(<參數(shù)>)<異常>)
(1)execution(* cn.itcast.aop.Book.add(..))
含義:任意修飾符,在cn.itcast.aop包下Book類中add方法(),有參數(shù)也包含
(2)execution(* cn.itcast.aop.Book.(..))
含義:任意修飾符,在cn.itcast.aop包下Book類中所有方法(),有參數(shù)也包含
(3)execution( .(..))
含義:任意修飾符,在所有類下所有方法,有參數(shù)也包含
(4) 匹配所有save開頭的方法 execution(* save*(..))
含義:任意修飾符,以save開頭的任意方法都增強(qiáng),有參數(shù)也包含
3 AspectJ支持5種類型的通知注解:
@Before:前置通知震束,在方法執(zhí)行之前返回
@After:后置通知怜庸,在方法執(zhí)行后執(zhí)行
@AfterRunning:返回通知,在方法返回結(jié)果之后執(zhí)行
@AfterThrowing:異常通知垢村,在方法拋出異常之后
@Around:環(huán)繞通知割疾,圍繞著方法執(zhí)行
package com.neuedu.aop;
public class Book {
public void add() {
System.out.println("add...........");
}
}
package com.neuedu.aop;
public class MyBook {
public void before1() {
System.out.println("前置增強(qiáng)......");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 1.配置對象 -->
<bean id="book" class="com.neuedu.aop.Book"></bean>
<bean id="myBook" class="com.neuedu.aop.MyBook"></bean>
<!-- 2.配置Aop操作 -->
<aop:config>
<!-- 2.1配置切入點(diǎn) -->
<aop:pointcut expression="execution(* com.neuedu.aop.Book.*(..))" id="qieru"/>
<!-- 2.2配置切面,把增強(qiáng)用到方法上面 -->
<aop:aspect ref="myBook">
<!-- 2.3配置增強(qiáng)類型method:增強(qiáng)類里面使用哪個方法作為前置-->
<aop:before method="before1" pointcut-ref="qieru"/>
</aop:aspect>
</aop:config>
</beans>
其他Aspectj的aop其他操作
package com.neuedu.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyBook {
public void before1() {
System.out.println("前置增強(qiáng)......");
}
public void after1() {
System.out.println("后置增強(qiáng)......");
}
//環(huán)繞通知
public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
//方法之前
System.out.println("方法之前...");
//執(zhí)行被增強(qiáng)的方法
proceedingJoinPoint.proceed();
//方法之后
System.out.println("方法之后...");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 1.配置對象 -->
<bean id="book" class="com.neuedu.aop.Book"></bean>
<bean id="myBook" class="com.neuedu.aop.MyBook"></bean>
<!-- 2.配置Aop操作 -->
<aop:config>
<!-- 2.1配置切入點(diǎn) -->
<aop:pointcut expression="execution(* com.neuedu.aop.Book.*(..))" id="qieru"/>
<!-- 2.2配置切面肝断,把增強(qiáng)用到方法上面 -->
<aop:aspect ref="myBook">
<!-- 2.3配置增強(qiáng)類型method:增強(qiáng)類里面使用哪個方法作為前置-->
<aop:before method="before1" pointcut-ref="qieru"/>
<!-- 后置增強(qiáng) -->
<aop:after-returning method="after1" pointcut-ref="qieru"/>
<!-- 環(huán)繞通知 -->
<aop:around method="around1" pointcut-ref="qieru"/>
</aop:aspect>
</aop:config>
</beans>