一、什么是AOP
AOP(Aspect Oriented Programming 面向切面编程),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。常用于日志记录,性能统计,安全控制,事务处理,异常处理等等。
二、AOP术语
连接点(Joinpoint) 程序执行的某个特定位置,如某个方法调用前,调用后,方法抛出异常后,这些代码中的特定点称为连接点。简单来说,就是在哪加入你的逻辑增强,连接点表示具体要拦截的方法,上面切点是定义一个范围,而连接点是具体到某个方法
切点(PointCut) 每个程序的连接点有多个,如何定位到某个感兴趣的连接点,就需要通过切点来定位。比如,连接点–数据库的记录,切点–查询条件,切点用于来限定Spring-AOP启动的范围,通常我们采用表达式的方式来设置,所以关键词是范围
增强(Advice) 增强是织入到目标类连接点上的一段程序代码。在Spring中,像BeforeAdvice等还带有方位信息。通知是直译过来的结果,我个人感觉叫做“业务增强”更合适,对照代码就是拦截器定义的相关方法,通知分为如下几种:
前置通知(before):在执行业务代码前做些操作,比如获取连接对象
后置通知(after):在执行业务代码后做些操作,无论是否发生异常,它都会执行,比如关闭连接对象
异常通知(afterThrowing):在执行业务代码后出现异常,需要做的操作,比如回滚事务
返回通知(afterReturning),在执行业务代码后无异常,会执行的操作
环绕通知(around),这个目前跟我们谈论的事务没有对应的操作,所以暂时不谈
目标对象(Target) 需要被加强的业务对象
织入(Weaving) 织入就是将增强添加到对目标类具体连接点上的过程。织入是一个形象的说法,具体来说,就是生成代理对象并将切面内容融入到业务流程的过程。
代理类(Proxy) 一个类被AOP织入增强后,就产生了一个代理类。
切面(Aspect) 切面由切点和增强组成,它既包括了横切逻辑的定义,也包括了连接点的定义,SpringAOP就是将切面所定义的横切逻辑织入到切面所制定的连接点中。
三。下面一个例子来表述Aop
dao层
package com.dao.imp;
public class AdminCheck {
public void check() {
System.out.println("正在验证账号信息");
}
}
daoimp层
package com.dao.imp;
import com.dao.BankDao;
public class BankDaoImp implements BankDao{
/*
private AdminCheck adminCheck;
private Transmaction transmaction;
public AdminCheck getAdminCheck() {
return adminCheck;
}
public void setAdminCheck(AdminCheck adminCheck) {
this.adminCheck = adminCheck;
}
public Transmaction getTransmaction() {
return transmaction;
}
public void setTransmaction(Transmaction transmaction) {
this.transmaction = transmaction;
}
*/
@Override
public void remit() {
// TODO Auto-generated method stub
// adminCheck.check();
// transmaction.beginTransmaction();
System.out.println("存钱业务逻辑");
// transmaction.closeTransmaction();
}
@Override
public void withdrawMoney() {
// TODO Auto-generated method stub
// adminCheck.check();
// transmaction.beginTransmaction();
System.out.println("取钱业务逻辑");
// transmaction.closeTransmaction();
}
@Override
public void transferAccounts() {
// TODO Auto-generated method stub
System.out.println("转账的业务逻辑");
}
}
daoimp检查账号
package com.dao.imp;
public class AdminCheck {
public void check() {
System.out.println("正在验证账号信息");
}
}
daoimp事务
package com.dao.imp;
import org.aspectj.lang.ProceedingJoinPoint;
public class Transmaction {
public void beginTransmaction() {
System.out.println("开始事务");
}
public void closeTransmaction() {
System.out.println("结束事务");
}
public void dointcepter(ProceedingJoinPoint point) throws Throwable {
beginTransmaction();
point.proceed();
closeTransmaction();
}
}
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="adminCheck" class="com.dao.imp.AdminCheck"></bean>
<bean id="transmaction" class="com.dao.imp.Transmaction"></bean>
<bean id="bankDao" class="com.dao.imp.BankDaoImp">
<!-- <property name="adminCheck" ref="adminCheck"></property>
<property name="transmaction" ref="transmaction"></property>-->
</bean>
<aop:config>
<!-- 切入点 什么时候,执行什么切入进来-->
<aop:pointcut expression="execution(* com.dao.imp.*.*(..))" id="savepoint"/>
<!-- 切面-用来做事情-执行业务逻辑前-你要做或执行什么事情 -->
<aop:aspect id="adminincepter" ref="adminCheck">
<aop:before method="check" pointcut-ref="savepoint"/>
</aop:aspect>
<aop:aspect id="transmactionincepter" ref="transmaction">
<aop:around method="dointcepter" pointcut-ref="savepoint"/>
</aop:aspect>
</aop:config>
</beans>
测试类
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dao.BankDao;
public class Test {
public static void main(String[] args) {
test2();
}
private static void test2() {
// TODO Auto-generated method stub
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
BankDao bd=ac.getBean("bankDao",BankDao.class);
bd.transferAccounts();
}
private static void test1() {
// TODO Auto-generated method stub
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
BankDao bd=ac.getBean("bankDao",BankDao.class);
bd.remit();
}
}