1.Aop 概述
面向方面的编程(AOP):通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。(百度百科)
其实说白一点就是一种新的编程思想,就像面向对象思想一样,其实直接把他当成拦截器也可以。
2.spring Aop
既然spring 引入了这个思想,那么就要具体实现,为了实现这个aop思想,spring 用了几个属于从不同方面描述了他,从而让人们知道什么是spring AOP 以及怎么使用!
术语 | 解释 |
---|---|
Aspect(切面) | 比如一对夫妇要生孩子,AOP 就是他们想要生孩子的想法,“Aspect” 使他们给生出的孩子的外国名字,“切面”是孩子的中文名字 |
Join point (连接点) | 相当于孩子的玩具,我们可以理解为类中的方法 |
Pointcut(切点) | 玩具很多,但是孩子只能玩一部分,所以你需要选择,就像代码中的(execution(* aop.Worker.eate(..))) |
Advice(通知) | 相当于孩子拿到了你给他选择的玩具,这个时候孩子就可以按照自己的兴趣玩耍了(Before,After..) |
Introduction(增强) | 如果他是个有想法的宝宝,也许会拆开玩具添加或者毁坏一些东西,相当于我门给切入的方法添加新的东西 |
Target object(目标对象) | 也就是我们具体方法和切面通过动态代理或者cglib 产生的最终结果 |
Weaving(编织) | 将我们的方法和我们的通知通过spring 动态代理或者cglib生成 Target object(目标对象),编织指的是编译时候的动态过程 |
- 我们在写代码的时候常用的是 Aspect(定义一个切面),Pointcut(切点)(切入那些方法),Advice(通知)(干什么)
3.举例
定义一个切面
package aop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* 工人切面
* @author LENOVO
*
*/
@Aspect
public class WorkerAspect {
//拦截worker类中的方法名字为 eate 的方法
@Pointcut("execution(* aop.Worker.eate(..))")
public void eateService() {
}
@Before("eateService()")
public void doBefore() {
System.out.println("食堂大妈在做饭,下面给你吃!");
}
@After("eateService()")
public void doAfter() {
System.out.println("面吃完啦,大妈洗碗了!");
}
//你也可以这样写,但是其他地方不能再引用了
/*@After("execution(* aop.Worker.eate(..))")
public void doAfter() {
System.out.println("面吃完啦,大妈洗碗了!");
}*/
}
定义一个接口
package aop;
public interface Eate {
public void eate();
}
定义具体实现类
package aop;
/**
* 工人类
* @author LENOVO
*
*/
public class Worker implements Eate {
@Override
public void eate() {
System.out.println("食堂开饭,食堂做的什么,我吃什么");
}
}
配置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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<!-- 开启切面 -->
<aop:aspectj-autoproxy/>
<bean id="myAspect" class="aop.WorkerAspect"></bean>
<bean id="worker" class="aop.Worker"> </bean>
</beans>
添加依赖jar
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
测试类
package aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class WorkerTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:Beans.xml");
Eate worker = (Eate) applicationContext.getBean("worker");
worker.eate();
}
}
结果
完!