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

了解Spring框架AOP(面向切面编程)

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

了解Spring框架AOP(面向切面编程)

AOP 【原理及代码实现】
  • 一、AOP在Spring中的作用
  • 二、使用Spring实现AOP
    • 方式一:使用Spring的API接口
    • 方法二:自定义类来实现AOP(主要是切面定义)
    • 方式三: 使用注解实现
  • 三、AOP优点

概念

AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP 是 OOP 的延续,是软件开发中的一个热点,也是 Spring 框架中的一个重要内容,是函数式编程的一种衍生范型。利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率

可以通过预编译方式和运行其动态代理实现在不修改源代码的情况下给程序动态统一添加某种特定功能,对我们的已有方法进行增强的一种技术。AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,提高代码的灵活性和可扩展性,AOP可以说也是这种目标的一种实现

一、AOP在Spring中的作用

提供声明式事务;允许用户自定义切面

以下名词需要了解下:

  • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存 ,事务等等 ….

  • 切面(ASPECT):横切关注点被模块化的特殊对象。即,它是一个类,封装的、用于横向插入系统的功能类

  • 通知 / 增强处理(Advice):切面必须要完成的工作。即,它是类中的一个方法,它是切面的具体实现,可以通知 / 增强处理

  • 目标(Target):被通知对象

  • 代理(Proxy):向目标对象应用通知之后,程序动态创建的对象

  • 连接点(JointPoint):与切入点匹配的执行点,是程序执行过程中某个特定的节点

  • 切入点(PointCut):切面通知执行的 地点的定义。在某个连接点满足预先指定的条件时, AOP 就能够定位到这个连接点,在连接点处插入切面,该连接点也就变成了切入点

  • 引介(Introduction):引介是一种特殊的通知,它可为目标对象添加一些属性和方法

  • 织入(Weaving):是将切面应用到目标对象的过程,这个过程可以是在编译时(例如使用 AspectJ 编译器),类加载时,运行时完成。Spring AOP 和其它纯 Java AOP 框架一样,是在运行时执行植入

SpringAOP中,通过Advice定义横切逻辑,Spring中支持5种类型的Advice

通知类型连接点实现接口
前置通知方法前org.springframework.aop.MethodBeforeAdvice
后置通知方法后org.springframework.aop.AfterReturningAdvice
环绕通知方法前后org.aopalliance.intercept.MethodInterceptor
异常抛出通知方法抛出异常org.springframework.aop.ThrowsAdvice
引介通知类中增加新的方法属性org.springframework.aop.IntroductionInterceptor

即 Aop 在 不改变原有代码的情况下,去增加新的功能 .

二、使用Spring实现AOP

【重点】使用AOP织入,需要到一个依赖包



    org.aspectj
    aspectjweaver
    1.9.8


方式一:使用Spring的API接口

AfterLog.class

public class AfterLog implements AfterReturningAdvice {

    //returnValue: 返回值
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法,返回结果为"+returnValue);
    }
}

Log.class

public class Log implements MethodBeforeAdvice {

    //method: 要执行的目标对象方法
    //args: 参数
    //target: 目标对象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
    }
}

接口和实现类

applicationContext.xml配置





    
    
    


    

    
    
        
        
        
        


        
        
        
    



文件目录

测试

public class Mytest {


    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        
        UserService userService = context.getBean("userService", UserService.class);

        userService.add();
    }
}


方法二:自定义类来实现AOP(主要是切面定义)

自定义一个类DiyPointCut.class

public class DiyPointCut {



    public void pointCut(){
        //配置切点
    }

    public void before(){
        System.out.print("方法执行前");
    }


    public void after(){
        System.out.println("方法执行后");
    }


    public void afterReturning(){
        System.out.println("方法返回后");
    }

    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前");

        //执行方法
        Object proceed = joinPoint.proceed();

        System.out.println("环绕后");
	}

    public void afterThrowing(){
        System.out.println("方法抛出异常");
    }
}

applicationContext.xml配置




	
    

    
    
    
    
        
        
            
            
            
            
            
            
            
                        
        
    

方式三: 使用注解实现

在DiyPointCut.class 类中添加注解

@Aspect
@Component
public class DiyPointCut {

        

    @Pointcut("execution(* com.vinjcent.service.UserServiceImpl.*(..))")
    public void pointCut(){
        //配置切点
    }

    @Before("pointCut()")
    public void before(){
        System.out.print("方法执行前");
    }


    @Before("pointCut()")
    public void after(){
        System.out.println("方法执行后");
    }


    @AfterReturning("pointCut()")
    public void afterReturning(){
        System.out.println("方法返回后");
    }

    @Around("pointCut()")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前");

        //执行方法
        Object proceed = joinPoint.proceed();

        System.out.println("环绕后");

    }

    @AfterThrowing("pointCut()")    
    public void afterThrowing(){
        System.out.println("方法抛出异常");
    }
}

applicationContext.xml配置




	
    

	
	
	
	


三、AOP优点
  • 业务代码更加简洁,例如当需要在业务行为前后做一些事情时候,只需要在该行为前后配置切面进行处理,无须修改业务行为代码,这也大大降低业务逻辑各部分之间的耦合度

  • 切面逻辑封装性好,并且可以被复用,例如我们可以把打日志的逻辑封装为一个切面,那么我们就可以在多个相关或者不相关的类的多个方法上配置该切面。提高了程序的可重用性,同时也提高了开发的效率

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

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

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