栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring框架-AOP

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring框架-AOP

一、概述 1. 基本介绍

AOP:面向切面编程,是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

作用:在程序运行期间,在不修改源码的情况下对方法进行功能增强。

底层实现:AOP 的底层是通过 Spring 提供的的动态代理技术实现的。在运行期间,Spring通过动态代理技术动态
的生成代理对象,代理对象方法执行时进行增强功能的介入,在去调用目标对象的方法,从而完成功能的增强。

2. 相关概念

Target(目标对象):代理的目标对象
Proxy(代理):一个类被 AOP 织入增强后,就产生一个结果代理类
Joinpoint(连接点):所谓连接点是指可以被增强的方法。在spring中这些点指的是方法,因为spring只支持方
法类型的连接点
Pointcut(切入点):所谓切入点是指已经被增强的方法。我们要对哪些 Joinpoint 进行拦截的定义
Advice(通知/ 增强):所谓通知是指作用是增强的方法。拦截到 Joinpoint 之后所要做的事情就是通知
Aspect(切面):增强类,是切入点和通知(引介)的结合
Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程。spring采用动态代理织入,而AspectJ采用编译期织入和类装载期织入

二、动态代理

动态代理的步骤

  1. 创建目标类接口和对象

    public interface TargetInterface {
    	public void method();
    }
    
    public class Target implements TargetInterface {
        @Override
        public void method() {
            System.out.println("Target running....");
        }
    }
    
  2. 实现动态代理

    public proxyTest() {
        Target target = new Target(); // 创建目标对象
        // 创建代理对象
        //参数:目标类加载器、目标类所有方法,目标类的增强处理
        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 {
                    System.out.println(" 前置增强代码...");
                    Object invoke = method.invoke(target, args);
                    System.out.println(" 后置增强代码...");
                    return invoke;
            	}
            }
        );
    }
    
    

常用的动态代理技术

  • JDK 代理 : 基于接口的动态代理技术
  • cglib 代理:基于父类的动态代理技术

在 spring 中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式

1. 基于JDK
  1. 创建目标类接口和对象

    public interface TargetInterface {
        public void save();
    }
    public class Target implements TargetInterface {
        public void save() {
            System.out.println("save running.....");
        }
    }
    
  2. 创建增强类

    public class Advice {
        public void before(){
            System.out.println("前置增强....");
        }
        public void afterReturning(){
            System.out.println("后置增强....");
        }
    }
    
  3. 实现动态代理

    public class ProxyTest {
        public static void main(String[] args) {
            //目标对象
            final Target target = new Target();
            //增强对象
            final Advice advice = new Advice();
            //返回值 就是动态生成的代理对象
            TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(
                target.getClass().getClassLoader(), //目标对象类加载器
                target.getClass().getInterfaces(), //目标对象所有方法
                new InvocationHandler() {
                    //调用代理对象的任何方法  实质执行的都是invoke方法
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        advice.before(); //前置增强
                        Object invoke = method.invoke(target, args);//执行目标方法
                        advice.afterReturning(); //后置增强
                        return invoke;
                    }
                }
            );
            //调用代理对象的方法
            proxy.save();
        }
    }
    
2. 基于cglib
  1. 创建目标类

    public class Target {
        public void save() {
            System.out.println("save running.....");
        }
    }
    
  2. 创建增强类

    public class Advice {
        public void before(){
            System.out.println("前置增强....");
        }
        public void afterReturning(){
            System.out.println("后置增强....");
        }
    }
    
  3. 实现动态代理

    public class ProxyTest {
        public static void main(String[] args) {
            //目标对象
            final Target target = new Target();
            //增强对象
            final Advice advice = new Advice();
            //1、创建增强器
            Enhancer enhancer = new Enhancer();
            //2、设置父类(即目标类)
            enhancer.setSuperclass(Target.class);
            //3、设置回调
            enhancer.setCallback(new MethodInterceptor() {
                public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
                    advice.before(); //执行前置
                    Object invoke = method.invoke(target, args);//执行目标
                    advice.afterReturning(); //执行后置
                    return invoke;
                }
            });
            //4、创建代理对象
            Target proxy = (Target) enhancer.create();
            proxy.save();
        }
    }
    
三、实现方式

开发内容

  1. 编写核心业务代码(目标类的目标方法)
  2. 编写切面类,切面类中有通知(增强功能方法)
  3. 在配置文件中,配置织入关系,即将哪些通知与哪些连接点进行结合
1. XML配置

基于xml的aop开发步骤:

  1. 导入 AOP 相关坐标:Spring的AOP框架底层使用的是aspectj

    
        
            org.springframework
            spring-context
            5.0.5.RELEASE
        
        
            org.aspectj
            aspectjweaver
            1.8.4
        
        
            org.springframework
            spring-test
            5.0.5.RELEASE
        
        
            junit
            junit
            4.12
        
    
    
  2. 创建目标接口和目标类(内部有切点)

    public interface TargetInterface {
        public void save();
    }
    
    public class Target implements TargetInterface {
        public void save() {
            System.out.println("save running.....");
            //int i = 1/0;
        }
    }
    
  3. 创建切面类(内部有增强方法)

    public class MyAspect {
        public void before(){
            System.out.println("前置增强..........");
        }
        public void afterReturning(){
            System.out.println("后置增强..........");
        }
        //Proceeding JoinPoint:  正在执行的连接点===切点
        public Object around(ProceedingJoinPoint pjp) throws Throwable {
            System.out.println("环绕前增强....");
            Object proceed = pjp.proceed();//切点方法
            System.out.println("环绕后增强....");
            return proceed;
        }
        public void afterThrowing(){
            System.out.println("异常抛出增强..........");
        }
        public void after(){
            System.out.println("最终增强..........");
        }
    }
    
  4. 将目标类和切面类的对象创建权交给 spring

    
    
    
    
  5. 在 applicationContext.xml 中配置织入关系

    
    
        
    
    
        
        
            
            
            
            -->
            
            
            
            
        
            
    
  6. 测试代码

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class AopTest {
        @Autowired
        private TargetInterface target;
    
        @Test
        public void test1(){
            target.save();
        }
    }
    
2. 注解配置

基于注解的aop开发步骤:

  1. 创建目标接口和目标类(内部有切点)

    public interface TargetInterface {
        public void save();
    }
    
    @Component("target")	//交给Spring容器
    public class Target implements TargetInterface {
        public void save() {
            System.out.println("save running.....");
            //int i = 1/0;
        }
    }
    
  2. 创建切面类(内部有增强方法),并在切面类中使用注解配置织入关系

    @Component("myAspect")	//交给Spring容器
    @Aspect //标注当前MyAspect是一个切面类
    public class MyAspect {
        //定义切点表达式
        @Pointcut("execution(* com.demo.anno.*.*(..))")
        public void pointcut(){}
        //配置前置通知
        @Before("execution(* com.demo.anno.*.*(..))")
        public void before(){
            System.out.println("前置增强..........");
        }
        //配置后置通知
        @AfterReturning("execution(* com.demo.anno.*.*(..))")
        public void afterReturning(){
            System.out.println("后置增强..........");
        }
        //配置环绕通知
        //Proceeding JoinPoint:  正在执行的连接点===切点
        @Around("pointcut()")
        public Object around(ProceedingJoinPoint pjp) throws Throwable {
            System.out.println("环绕前增强....");
            Object proceed = pjp.proceed();//切点方法
            System.out.println("环绕后增强....");
            return proceed;
        }
    	//配置异常抛出通知
        @AfterThrowing("pointcut()")
        public void afterThrowing(){
            System.out.println("异常抛出增强..........");
        }
    	//配置最终通知
        @After("MyAspect.pointcut()")
        public void after(){
            System.out.println("最终增强..........");
        }
    }
    
  3. 在配置文件中开启组件扫描和 AOP 的自动代理

    
    
    
        
        
    
        
        
    
    
    
  4. 测试

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext-anno.xml")
    public class AnnoTest {
        @Autowired
        private TargetInterface target;
    
        @Test
        public void test1(){
            target.save();
        }
    }
    
四、事务

**Spring 的声明式事务就是用在 Spring 配置文件中声明式的处理事务来代替代码式的处理事务。**Spring 声明式事务控制底层就是AOP。

1. 编程式事务控制的对象

编程式事务控制三大对象

  • PlatformTransactionManager
  • TransactionDefinition
  • TransactionStatus

PlatformTransactionManager接口是spring 的事务管理器,不同的 Dao 层技术有不同的实现类:jdbc
或 mybatis时使用DataSourceTransactionManager,hibernate时使用HibernateTransactionManager

  • getTransaction()获取事务的状态信息
  • commit()提交事务
  • rollback()回滚事务

TransactionDefinition是事务的定义信息对象

  • getIsolationLevel() 获取事务隔离级别
    • ISOLATION_DEFAULT
    • ISOLATION_READ_UNCOMMITTED
    • ISOLATION_READ_COMMITTED
    • ISOLATION_REPEATABLE_READ
    • ISOLATION_SERIALIZABLE
  • getPropogationBehavior() 获取事务传播行为
    • REQUIRED:如果当前没有事务就新建一个事务,如果已经存在一个事务中加入到这个事务中。一般选择默认值
    • SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务)
    • 超时时间:默认值是-1,没有超时限制。如果有,以秒为单位进行设置
    • 是否只读:建议查询时设置为只读
  • getTimeout() 获得超时时间
  • isReadonly() 是否只读

TransactionStatus 接口提供的是事务具体的运行状态

  • hasSavepoint()是否存储回滚点
  • isCompleted() 事务是否完成
  • isNewTransaction() 是否是新事务
  • isRollbackonly() 事务是否回滚
2. xml声明式事务控制

XML声明式事务控制的实现步骤

  1. 引入tx命名空间

    
    
  2. 编写目标对象,交给Spring管理

    public interface AccountService {
        public void transfer(String outMan,String inMan,double money);
    
    }
    
    public class AccountServiceImpl implements AccountService {
        private AccountDao accountDao;
        public void setAccountDao(AccountDao accountDao) {
            this.accountDao = accountDao;
        }
        public void transfer(String outMan, String inMan, double money) {
            accountDao.out(outMan,money);
            int i = 1/0;
            accountDao.in(inMan,money);
        }
    }
    
    
        
    
    
  3. 配置事务增强

    
        
    
    
    
    
        
        
            
            
            
            
            
            
        
    
    
  4. 配置事务 AOP 织入

    
        
        	
        
    
    
  5. 测试

    public class AccountController {
        public static void main(String[] args) {
            ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
            AccountService accountService = app.getBean(AccountService.class);
            accountService.transfer("tom","lucy",500);
        }
    }
    
3. 注解声明式事务控制
  1. 编写目标类

    public interface AccountService {
        public void transfer(String outMan,String inMan,double money);
    
    }
    
    @Service("accountService")
    @Transactional(isolation = Isolation.REPEATABLE_READ)	//设置事务的属性信息
    public class AccountServiceImpl implements AccountService {
        @Autowired
        private AccountDao accountDao;
    
        //当类与方法都设置事务属性信息,采用就近原则
        @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
        public void transfer(String outMan, String inMan, double money) {
            accountDao.out(outMan,money);
            int i = 1/0;
            accountDao.in(inMan,money);
        }
    }
    
    
  2. 编写配置文件

    
    
    
        
        
    
    	
        
            
        
    
        
        
    
    
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/671547.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号