Spring AOP 简介
AOP : Aspect Oriented Program 面向切面编程
首先,在面向切面编程的思想里把功能分为核心业务功能,和周边功能。
- 核心业务:登录,增加数据,删除数据等都叫核心业务
- 周边功能:性能统计,日志,事务管理等
周边功能在Spring的面向切面编程AOP思想里,被定义为切面。
AOP:核心业务功能和且米娜功能分别独立进行开发,然后把切面功能和核心业务功能编织在一起。
AOP 的目的
AOP能够将那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可拓展性和可维护性。
AOP中的概念:
- 切入点(Pointcut):在哪些类,哪些方法上切入(where)
- 通知(Advice):在方法执行的什么时机(when)做什么(what)
- 切面(Aspect):=切入点 + 通知
- 织入(Weaving):把切面加入到对象,并创建出代理对象的过程。(由Spring完成)
一个例子
AOP的一个思想:将关注点代码和业务代码分离!
代码:
- 在Package【pojo】下新建包租婆类:
package pojo;
import org.springframework.stereotype.Component;
@Component("landlord")
public class Landlord {
public void service() {
// 仅仅只是实现了核心的业务功能
System.out.println("签合同");
System.out.println("收房租");
}
}
- 在Package【aspect】下新建一个中介商【Broker】类:
package aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
class Broker {
@Before("execution(* pojo.Landlord.service())")
public void before(){
System.out.println("带租客看房");
System.out.println("谈价格");
}
@After("execution(* pojo.Landlord.service())")
public void after(){
System.out.println("交钥匙");
}
}
- 在applicationContext.xml中配置自动注入,并告诉Spring IoC容器去哪里扫描着两个Bean:
<?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="aspect" />
<context:component-scan base-package="pojo" />
<aop:aspectj-autoproxy/>
</beans>
- 在Package【test】下编写测试代码:
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Landlord;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Landlord landlord = (Landlord) context.getBean("landlord", Landlord.class);
landlord.service();
}
}
- 执行看效果:
可以看到,我们在Landlord的service()方法中仅仅实现了核心的业务代码,其余的关注点功能是根据我们设置的功能自动补全的。
使用注解来开发 Spring AOP
使用注解的方式已经逐渐成为了主流,以上例来说明如何用注解来开发Spring AOP
-
选择连接点
Spring是方法级别的AOP框架,选择连接点就是选择哪一个类的哪一个方法用以增强功能。 -
创建切面
可以把切面理解为一个拦截器,当程序运行到连接点的时候,被拦截下来,在开头加入了初始化的方法,在结尾也加入了销毁的方法而已。在Spring中只要使用@Aspect
注解一个类,那么Spring IoC容器就会认为这是一个切面了:package aspect; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Component @Aspect class Broker { @Before("execution(* pojo.Landlord.service())") public void before(){ System.out.println("带租客看房"); System.out.println("谈价格"); } @After("execution(* pojo.Landlord.service())") public void after(){ System.out.println("交钥匙"); } }
注意:被定义为切面的类仍然是一个Bean,需要
@Component
注解标注Spring中的Aspect注解:
解 说明 @Before
前置通知,在连接点方法前调用 @Around
环绕通知,它将覆盖原有方法,但是允许你通过反射调用原有方法 @After
后置通知,在连接点方法后调用 @AfterReturning
返回通知,在连接点方法执行并正常返回后调用,要求连接点方法在执行过程中没有发生异常 @AfterThrowing
异常通知,当连接点方法异常时调用 -
定义切点
在上面的注解中定义了exxcution的正则表达式,Spring通过这个正则表达式判断具体要拦截的是哪一个类的哪一个方法:
execution(* pojo.Landlord.service())
依次分析:- execution:代表执行方法的时候会触发
*
:代表任意返回类型的方法- pojo.Landlord:代表类的全限定名
- service():被拦截的方法名称
通过上面的表达式,Spring就会知道应该拦截pojo.Landlord类下的service()方法。上面的演示类还好,如果多处都需要写这样的表达式难免会有些复杂,我们可以通过使用@Pointcut
注解定义一个切点来避免这样的麻烦:
package aspect; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Component @Aspect class Broker { @Pointcut("execution(* pojo.Landlord.service())") public void lService() { } @Before("lService()") public void before() { System.out.println("带租客看房"); System.out.println("谈价格"); } @After("lService()") public void after() { System.out.println("交钥匙"); } }
- 测试AOP
编写测试代码
环绕通知
这是 Spring AOP 中最强大的通知,因为它集成了前置通知和后置通知,它保留了连接点原有的方法的功能,所以它既强大又灵活,让我们来看看:
package aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
@Aspect
class Broker {
// 注释掉之前的 @Before 和 @After 注解以及对应的方法
// @Before("execution(* pojo.Landlord.service())")
// public void before() {
// System.out.println("带租客看房");
// System.out.println("谈价格");
// }
//
// @After("execution(* pojo.Landlord.service())")
// public void after() {
// System.out.println("交钥匙");
// }
// 使用 @Around 注解来同时完成前置和后置通知
@Around("execution(* pojo.Landlord.service())")
public void around(ProceedingJoinPoint joinPoint) {
System.out.println("带租客看房");
System.out.println("谈价格");
try {
joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("交钥匙");
}
}
运行测试代码,结果仍然正确。
使用XML配置开发 Spring AOP
注解是很强大的东西,但基于XML的开发我们仍然需要了解,AOP中可以配置的元素:
XML配置如下:(去掉注解)
<!-- 装配 Bean-->
<bean name="landlord" class="pojo.Landlord"/>
<bean id="broker" class="aspect.Broker"/>
<!-- 配置AOP -->
<aop:config>
<!-- where:在哪些地方(包.类.方法)做增加 -->
<aop:pointcut id="landlordPoint"
expression="execution(* pojo.Landlord.service())"/>
<!-- what:做什么增强 -->
<aop:aspect id="logAspect" ref="broker">
<!-- when:在什么时机(方法前/后/前后) -->
<aop:around pointcut-ref="landlordPoint" method="around"/>
</aop:aspect>
</aop:config>