AOP 简介
面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是 Spring
框架中的一个重要内容。利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度
降低,提高程序的可重用性,同时提高了开发的效率。
主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理等等。
Spring AOP实例
1,前置通知;2,后置通知;3,环绕通知;4,返回通知;5,异常通知;
涉及到切面,需要导入以下jar包:
在beans.xml中引入AOP相关内容
<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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="studentServiceAspect" class="com.java1234.advice.StudentServiceAspect"></bean>
<bean id="studentService" class="com.java1234.service.impl.StudentServiceImpl"></bean>
<!-- AOP配置 -->
<aop:config>
<!-- 定义切面,ref指向切面类 -->
<aop:aspect id="studentServiceAspect" ref="studentServiceAspect">
<!-- 定义切点,定义到方法级,expression方法表达式 第一个*表示方法的返回值任意 括号中的..代表方法的参数任意-->
<aop:pointcut expression="execution(* com.java1234.service.*.*(..))" id="businessService"/>
<!-- 定义前置通知,切面类的doBefore方法,pointcut-ref指向切点-->
<aop:before method="doBefore" pointcut-ref="businessService"/>
<aop:after method="doAfter" pointcut-ref="businessService"/><!-- 定义后置通知 -->
<aop:around method="doAround" pointcut-ref="businessService"/><!-- 定义环绕通知 -->
<aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/><!-- 定义返回通知 -->
<aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/><!-- 定义异常通知 注意要配置throwing -->
</aop:aspect>
</aop:config>
</beans>
定义接口StudentService,声明方法public void addStudent(String name);
接口实现类StudentServiceImpl
@Override
public void addStudent(String name) {
System.out.println("添加学生"+name);
// System.out.println(1/0);
}
切面类StudentServiceAspect
package com.java1234.advice;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class StudentServiceAspect {
//前置通知
public void doBefore(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("参数名:"+jp.getArgs()[0]);
}
//后置通知
public void doAfter(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("学生添加完成:"+jp.getArgs()[0]);
}
//环绕通知 可以有返回值 可能引发异常
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("添加学生前");
Object retVal=pjp.proceed();
System.out.println(retVal);
System.out.println("添加学生后");
return retVal;
}
//返回通知 在方法将要执行结束 return的时候执行
public void doAfterReturning(JoinPoint jp){
System.out.println("返回通知");
}
//异常通知
public void doAfterThrowing(JoinPoint jp,Throwable ex){
System.out.println("异常通知");
System.out.println("异常信息:"+ex.getMessage());
}
}
测试类
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
StudentService studentService=(StudentService)ac.getBean("studentService");
studentService.addStudent("张三");
}