• Spring-AOP


    Aop简介

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

    常见的定义解析

    切面:  关注点的模块化,将所有的符合切点代码统称为切面。

    切点: 植入我们自己定义的外部代码的表达式(切点表达式)com.ujiuye.service.*.*

    连接点:程序在调用目标方法的时候,如果这个方法符合切点表达式,自动的引入外部我们定义功能代码(当前正在调用的方法就是连接点)

    通知: before  after  around  exception  最终通知(调用的符合表达式,通过通知来决定什么时候执行外部的文件)

    织入:将当前正在调用的方法和外部的目标代码结合起来的过程就是织入。

    目标对象: 被代理的对象,当前的正在执行的这个方法的所属对象。

    代理: aop的核心的设计模式就是动态代理模式。

     

     

    AOP 名词解释

    JoinPoint( 连接点 ): 在目标对象中,所有可以增强的方法。

    PointCut( 切入点 ):目标对象,已经增强的方法。

    Advice( 通知 / 增强 ):增强的代码

    Target( 目标对象 ):被代理对象

    WeAVing( 织入 ): 将通知应用到切入点的过程

     

    前置通知before 目标方法之前调用

    后置通知afterReturning (如果出现异常不会调用) 在目标方法运行之后

    环绕通知around 在目标方法之前和之后都调用

    异常拦截通知afterThrowing 如果出现异常,就会调用

    后置通知after(无论是否出现异常都会调用)在目标方法运行之后调用

     

    Spring-Aop具体的应用

    1.导包

    2.切面

    public class MyAspect {

     

     

        public void before(){

            System.out.println("这是前置通知");

        }

     

        public Object around(ProceedingJoinPoint  point) throws Throwable {

            System.out.println("这是环绕通知之前");

            Object obj = point.proceed();

            System.out.println("这是环绕通知之后");

            return obj;

        }

     

        public void after(){

            System.out.println("这是后置通知(无论是否出现异常都会执行)");

        }

     

        public void afterReturnning(){

            System.out.println("这是后置通知,(如果有异常不走)");

        }

     

        public void afterThrowing(){

            System.out.println("捕获到了异常");

     

        }

    }

     

    Bean.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <beans  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                      xmlns="http://www.springframework.org/schema/beans"

                      xmlns:context="http://www.springframework.org/schema/context"

                      xmlns:aop="http://www.springframework.org/schema/aop"

                      xmlns:tx="http://www.springframework.org/schema/tx"

                      xsi:schemaLocation="http://www.springframework.org/schema/beans

                                          http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

                                                                   http://www.springframework.org/schema/context

                                                                   http://www.springframework.org/schema/context/spring-context-4.2.xsd

                                                                   http://www.springframework.org/schema/aop

                                                                   http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

                                                                   http://www.springframework.org/schema/tx

                                                                   http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

    <!--   配置切面-->

             <bean id="aspect" class="com.ujiuye.aspect.MyAspect"/>

             <!--aop相关配置-->

             <aop:config>

                      <!--配置切入点  expression  第一个*代表返回值类型,第二个*代表类名的通配符 ,第三个*代表方法名,..参数列表-->

                      <aop:pointcut id="pt" expression="execution(* com.ujiuye.service.*ServiceImpl.*(..))"/>

                      <aop:aspect ref="aspect">

                               <!--后置通知-->

                               <aop:after method="after" pointcut-ref="pt"/>

                               <!--前置通知-->

                               <aop:before method="before" pointcut-ref="pt"/>

                               <!--环绕通知-->

                               <aop:around method="around" pointcut-ref="pt"/>

                               <!--捕获异常-->

                               <aop:after-throwing method="afterThrowing" pointcut-ref="pt"/>

                               <!--后置通知,遇到异常不会执行-->

                               <aop:after-returning method="afterReturnning" pointcut-ref="pt"/>

                      </aop:aspect>

             </aop:config>

     

             <bean id="personService" class="com.ujiuye.service.PersonServiceImpl">

                      <property name="dao" ref="personDao"/>

             </bean>

             <bean id="personDao" class="com.ujiuye.dao.PersonDaoImpl"/>

    </beans>

     

    测试:

    @Test

        public void test1(){

            ApplicationContext app=new ClassPathXmlApplicationContext("beans.xml");

           PersonService service= (PersonService) app.getBean("personService");

           //service.del();

            service.save();

        }

  • 相关阅读:
    web.xml报错
    mysql字符集问题汇总
    查询所有表中的某个数存储过程脚本
    SQL Server生成数据库的数据字典存储过程
    浏览器无法访问虚拟机的服务器
    搭建lnmp环境,nginx的配置文件/etc/nginx/nginx.conf
    在centos6.5下搭建lnmp
    Linux服务器关联Git,通过执行更新脚本实现代码同步
    CentOS yum 安装时错误 Errno 14 Couldn't resolve host 解决办法
    LinqToSQL3
  • 原文地址:https://www.cnblogs.com/masterhxh/p/12957620.html
Copyright © 2020-2023  润新知