Spring_AOP基于xml配置和注解配置
一,什么是AOP
AOP为Aspect Oriented Programming的缩写,意思是面向切面编程,是通过预编译的方式和运行期动态代理实现程序功能的统一维护的一项技术.典型的应用就是spring事务管理,通过切面编程为service层中的方法进行统一的事务管理,将service层中的事务管理代码抽取,统一管理,达到解耦的目的
二,AOP的作用和优势
- 作用:在程序运行期间,在不修改源码的情况下对方法进行功能增强
- 优势:减少重复代码,提高开发效率,并且便于维护
三,AOP底层分析
aop的底层是用动态代理技术实现的,常见的动态代理技术有两种,在spring中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式
1.JDK代理:基于接口的动态代理技术(通过目标对象实现的接口,动态代理一个功能增强的"兄弟")
/*
* 基于jdk的动态代理,基于接口
*
*
* 三个参数
* 第一个为目标对象类加载器
* 第二个为目标对象相同的接口字节码对象数组
* 第三个为一个接口
* */
public class ProxyTest {
public static void main(String[] args) {
//目标对象
Target target = new Target();
//增强对象
Advice advice = new Advice();
//返回值为动态代理生成的代理对象,与目标对象是兄弟关系,用父类接口接收
TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//执行目标方法 两个参数,一个为目标对象,一个为参数
advice.before(); //前置增强
method.invoke(target,args);
advice.after(); //后置增强
return null;
}
}
);
//调用代理对象的方法
proxy.save();
}
}
2.cglib代理:基于父类的动态代理技术(通过目标对象作为父类,动态代理一个功能增强的"儿子")
public class ProxyTest {
public static void main(String[] args) {
//目标对象
Target target = new Target();
//增强对象
Advice advice = new Advice();
//返回值 就是动态生成的代理对象,基于cglib
//1.创建增强器
Enhancer enhancer = new Enhancer();
//2.设置父类(目标)
enhancer.setSuperclass(Target.class);
//3.设置回调函数
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
//执行前置
advice.before();
//执行目标
Object invoke = method.invoke(target, objects);
//执行后置
advice.after();
return invoke;
}
});
//4.创建代理对象
Target proxy = (Target) enhancer.create();
//测试
proxy.save();
}
}
四,AOP开发明确的事项和知识要点
- aop:面向切面编程
- aop底层实现:基于JDK的动态代理和基于Cglib的动态代理
- aop的重要概念:
- Pointcut(切点):被增强的方法
- Advice(通知/增强):封装增强业务逻辑的方法
- Aspect(切面):切点+通知
- Weaving(织入):将切点和通知结合的过程
- 开发明确事项:
- 谁是切点(切点表达式配置)
- 谁是通知(切面类中的增强方法)
- 将切点和通知进行织入配置
五,xml方式实现aop-快速入门
- 导入AOP相关坐标
- 创建目标接口和目标类(内部有切点)
- 创建切面类(内部有增强方法)
- 将切面类和目标类的对象创建权交给spring
- 在spring核心配置文件applicationContext.xml中配置织入关系
- 测试代码
xml配置相关标签(注意要引入aop命名空间)
<aop:config> <!--配置织入 -->
<aop:aspect ref="切面类"> <!--声明切面 -->
<aop:pointcut id="pointcut" expression=" execution(* com.xxxx.aop.*.*(..))"></aop:pointcut>
<aop:before method="增强方法的名称" pointcut-ref="pointcut"></aop:before>
</aop:aspect>
</aop:config>
切点表达式
pointcut="execution(* com.xxxx.*.*(..))" <!--任意返回值类型 com任意包下的任意类的任意(带任意参数的)方法 -->
<?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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!--将目标对象交给spring-->
<bean id="target" class="com.xxxx.aop.Target"></bean>
<!--将切面对象交给spring-->
<bean id="myAspect" class="com.xxxx.aop.MyAspect"></bean>
<!--配置织入 告诉spring框架,哪些方法(切点)需要进行哪些增强(前置,后置)-->
<aop:config>
<!--声明切面-->
<aop:aspect ref="myAspect">
<!--切点表达式的抽取-->
<aop:pointcut id="pointcut" expression=" execution(* com.xxxx.aop.*.*(..))"></aop:pointcut>
<!--切点织入通知-->
<!--<aop:before method="before" pointcut="execution(public void com.xxxx.aop.Target.save())"></aop:before>-->
<!--<aop:before method="before" pointcut="execution(* com.xxxx.aop.*.*(..))"></aop:before>
<aop:after-returning method="afterReturning" pointcut="execution(* com.xxxx.aop.*.*(..))"></aop:after-returning>-->
<!-- <aop:around method="around" pointcut="execution(* com.xxxx.aop.*.*(..))"></aop:around>
<aop:after-throwing method="afterThrowing" pointcut="execution(* com.xxxx.aop.*.*(..))"></aop:after-throwing>
<aop:after method="after" pointcut="execution(* com.xxxx.aop.*.*(..))"></aop:after>-->
<aop:around method="around" pointcut-ref="pointcut"></aop:around>
</aop:aspect>
</aop:config>
</beans>
xml配置aop开发小结
四个标签一个切点表达式
六,注解方式实现aop-快速入门
-
创建目标接口和目标类(内部有切点)
-
创建切面类(内部有增强方法)
-
将目标类和切面类的对象创建权交给spring
-
在切面类中使用注解配置织入关系
-
在配置文件中开启组件扫描和aop自动代理
<context:component-scan base-package="com.xxxx"></context:component-scan> <aop:aspectj-autoproxy />
-
测试
注解配置相关注解
1.使用在类上面
@Aspect:声明这是一个切面类
@Aspect
public class MyAspect{
}
2.使用在方法上面
@Before:前置通知,指定增强的方法在切入点方法之前执行
@Before("")
public void before(){
System.out.println("前置通知...");
}
@AfterReturning:后置通知,指定增强的方法在切入点方法之后执行
@AfterReturning("")
public void afterReturning(){
System.out.println("后置通知...");
}
@Around:环绕通知,在切入点方法之前和之后都执行
@Around("")
public void around(){
System.out.println("环绕通知...");
}
@AfterThrowing:用于配置异常抛出通知,指定增强的方法在出现异常时执行
@AfterThrowing("")
public void afterThrowing(){
System.out.println("异常通知...");
}
@After:最终通知,无论增强方法执行与否,无论是否有异常都会执行
@After("")
public void after(){
System.out.println("最终通知...")
}
@Pointcut:定义切点表达式,然后在增强注解中引用
@Component("myAspect")
@Aspect
public class MyAspect {
@Before("MyAspect.myPoint()")
public void before(){
System.out.println("前置代码增强.....");
}
@Pointcut("execution(* com.xxxx.aop.*.*(..))")
public void myPoint(){}
}
注解aop开发小结
切面类中的七个注解和xml中的两个标签.