AOP的一些概念
什么是AOP
AOP是面向切面编程,当你需要对原有功能进行扩展的时候,不需要修改源代码就可以实现。AOP采用横向抽取机制,取代了传统的纵向的继承体系。
AOP的相关术语
连接点:类里面可以被增强的方法称为连接点。
切入点:类里面实际被增强的方法称为切入点。
通知/增强:扩展的功能称为增强。
*前置增强:在方法执行之前增强
*后置增强:值方法执行之后增强
*异常增强:在方法出现异常时增强
*最终增强:在后置之后执行增强
*环绕增强:在方法执行之前和之后增强
切面:把增强应用到具体的方法上的过程称为切面
AOP的操作
在spring里面进行AOP操作,使用AspectJ实现,而使用AspectJ实现AOP有两种方式:基于AspectJ的XML配置和基于AspectJ的注解方式。
(1)导入jar包:除了基本的jar包之外,还需要导入AOP相关的jar包(前面有)。
(2)引入约束:这里需要引入spring-aop的约束。
(3)使用表达式配置切入点
常用的表达式
execution([方法访问修饰符] 方法返回值 包名.类名.方法名(方法的参数))
可以使用通配符“ * ”
下面具体的看一下AOP相关操作:
XML方式
(一)前置增强
步骤一:创建两个类,一个是增强类,一个是被增强类。
public class Book {
//被增强的类
public void add() {
System.out.println("Book...add......");
}
}
public class MyBook {
//增强类
public void before() {
System.out.println("MyBook...前置增强......");
}
}
步骤二:完成配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 1 创建两个类的对象 -->
<bean id="bookID" class="Book类全路径"></bean>
<bean id="mybookID" class="MyBook类全路径"></bean>
<!-- 2 配置aop操作 -->
<aop:config>
<!-- 2.1 配置切入点 -->
<aop:pointcut expression="execution(* 包名.Book.*(..))" id="pointcutAdd"/>
<!-- 配置切面:把增强用到方法上面 -->
<aop:aspect ref="mybookID">
<!-- 2.2 配置增强类型
method:增强类中使用哪个方法作为前置
-->
<aop:before method="before" pointcut-ref="pointcutAdd"/>
</aop:aspect>
</aop:config>
</beans>
步骤三:测试类进行测试
public class Test {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Book book = (Book) context.getBean("bookID");
book.add();
}
}
运行结果:
(二)后置增强
步骤一:创建两个类,一个是增强类,一个是被增强类。
public class Book {
//被增强的类
public void add() {
System.out.println("Book...add......");
}
}
public class MyBook {
//增强类
public void after() {
System.out.println("MyBook...后置增强......");
}
}
步骤二:完成配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 1 创建两个类的对象 -->
<bean id="bookID" class="Book类全路径"></bean>
<bean id="mybookID" class="包名.MyBook"></bean>
<!-- 2 配置aop操作 -->
<aop:config>
<!-- 2.1 配置切入点 -->
<aop:pointcut expression="execution(* 包名.Book.*(..))" id="pointcutAdd"/>
<!-- 配置切面:把增强用到方法上面 -->
<aop:aspect ref="mybookID">
<!-- 2.2 配置增强类型
method:增强类中使用哪个方法作为前置
-->
<!-- <aop:after method="after" pointcut-ref="pointcutAdd"/>
</aop:aspect>
</aop:config>
</beans>
步骤三:测试类
public class Test {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Book book = (Book) context.getBean("bookID");
book.add();
}
}
运行结果:
(三)环绕增强
步骤一:创建两个类,一个是增强类,一个是被增强类。
public class Book {
//被增强的类
public void add() {
System.out.println("Book...add......");
}
}
public class MyBook {
//增强类
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕之前......");
proceedingJoinPoint.proceed();
System.out.println("环绕之后......");
}
}
步骤二:完成配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 1 创建两个类的对象 -->
<bean id="bookID" class="Book类全路径"></bean>
<bean id="mybookID" class="MyBook类全路径"></bean>
<!-- 2 配置aop操作 -->
<aop:config>
<!-- 2.1 配置切入点 -->
<aop:pointcut expression="execution(* 包名.Book.*(..))" id="pointcutAdd"/>
<!-- 配置切面:把增强用到方法上面 -->
<aop:aspect ref="mybookID">
<!-- 2.2 配置增强类型
method:增强类中使用哪个方法作为前置
-->
<!-- <aop:around method="around " pointcut-ref="pointcutAdd"/>
</aop:aspect>
</aop:config>
</beans>
步骤三:测试类
public class Test {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Book book = (Book) context.getBean("bookID");
book.add();
}
}
运行结果:
注解方式
这里只详细讲解前置增强
步骤一:创建两个类,一个是增强类,一个是被增强类。
public class Book {
public void book() {
System.out.println("Book...被增强......");
}
}
public class MyBook {
//在方法上面使用注解完成配置
@Before(value="execution(* 包名.Book.*(..))")
public void mybook() {
System.out.println("MyBook...前置增强......");
}
}
步骤二:完成配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启AOP操作 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="bookID" class="Book类的全路径"></bean>
<bean id="mybookID" class="MyBook类的全路径"></bean>
</beans>
步骤三:在增强类上面完成注解
@Aspect
public class MyBook {
//在方法上面使用注解完成配置
@Before(value="execution(* 包名.Book.*(..))")
public void mybook() {
System.out.println("MyBook...前置增强...");
}
}
运行结果: