- AOP 面向切面编程,通过预编译的方式和运行期间动态代理实现程序功能的统一维护的一种技术,AOP 时OOP的延续。
- 作用 : 利用AOP可以对业务逻辑的各个部分进行隔离,从而是的业务逻辑的各个部分耦合性降低,提高程序的可用性,同时提高了开发的效率。
- 通俗解释: 不通过修改源代码的方式,在主干里进行功能的添加和删除
APO实现原理:动态代理
两种情况
-
有接口情况,使用jdk动态代理,
创建接口实现类代理对象,增强实现类的方法
-
没有接口情况: 使用CGLIB动态代理
创建子类的代理对象,增强类的方法
AOP(jdk动态代理)
准备创建一个方法的接口和方法的实现类,一个jdkInvacationHandler
package com.Zhibin.dao;
public interface UserDao {
public int add(int a,int b);
public String update(String id);
}
package com.Zhibin.dao;
public class UserDaoImpl implements UserDao{
@Override
public int add(int a, int b) {
return a + b;
}
@Override
public String update(String id) {
return id;
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JDKProxy {
// 创建接口实现类代理对象
public static void main(String[] args) {
Class[] interfaces = {UserDao.class};
// 创建代理对象
UserDao userDao = new UserDaoImpl();
UserDao dao =(UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(),interfaces,new UserDaoProxy(userDao));
System.out.println("result=" + dao.add(1,2));
}
}
class UserDaoProxy implements InvocationHandler{
private Object obj;
// 创建谁的代理对象,需要把谁传过来
// 通过有参构造传递
public UserDaoProxy(Object obj){
this.obj = obj;
}
// 增强的逻辑
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 方法之前执行
System.out.println("before method");
// 被增强的方法的执行
Object res = method.invoke(obj,args);
//方法之后
System.out.println("方法之后"+obj);
return res;
}
}
AOP操作(AspectJ)
- spring 框架一般都是基于AspecctJ实现AOP操作
- AspectJ 不是Spring组成部分,独立AOP框架,一般把AspectJ和spring框架一起使用,及逆行AOP操作
基于AspectJ实现AOP操作
- 基于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: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="com.Zhibin"></context:component-scan>
<!-- 开启生成代理对象 作用就是在扫描是寻找是否有Aspect标签-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
或者使用配置类
package config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan(basePackages = "com.Zhibin")
@EnableAspectJAutoProxy
public class config {
}
-
开启注解扫描
-
使用注解创建User 和 UserProxy对象 并使用@Component 标签对类进行标注
package com.Zhibin.Apo; import org.springframework.stereotype.Component; @Component public class User { // 被增强的方法 public void add(){ System.out.println("add..............."); } }
-
在增强类上面添加@Aspect 注解 (UserProxy)
-
在Spring配置文件中生成代理对象
-
配置不同类型的通知
-
在增强类里边在作为通知方法上面添加通知添加通知类型注解,使用切入点表达式配置
package com.Zhibin.Apo; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Aspect // 生成代理对象 @Component @Order(1) // 使用Order 来设置多个代理对象的优先级 public class UserProxy { // 相同切入点抽取 @Pointcut(value = "execution(* com.Zhibin.Apo.User.add())") public void pointdemo(){ } // before 注解 表示作为前置方法 @Before(value = "pointdemo()") public void before(){ System.out.println("before............."); }// 方法之前执行 @After(value = "execution(* com.Zhibin.Apo.User.add(..))") public void after(){ System.out.println("after............"); }//方法后执行 @AfterReturning(value = "execution(* com.Zhibin.Apo.User.add())") public void AfterReturning(){ System.out.println("afterreturning............."); }// 该方法结束后执行 @AfterThrowing(value = "execution(* com.Zhibin.Apo.User.add())") public void AfterThrowing(){ System.out.println("throwing........"); }// 报错抛出异常时执行 @Around(value = "execution(* com.Zhibin.Apo.User.add())") public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {// 在执行Around 标签的方法时需要有ProceedingJoinPoint 参数, 这个参数获取的是 原来的方法 // Around 在方法开始前和开始后都执行 System.out.println("this is around..........."); proceedingJoinPoint.proceed(); System.out.println("this is around........"); } }
@Test public void testAop(){ ApplicationContext context = new AnnotationConfigApplicationContext(config.class); User user = (User)context.getBean("user", User.class); user.add(); } /* this is around........... before............. add............... this is around........ after............ afterreturning............. 测试结果
-