zoukankan      html  css  js  c++  java
  • AOP

    AOP

    1.什么是AOP

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

    2.AOP在Spring中的作用

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

    • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等....
    • 切面(ASPECT):横切关注点被模块化的特殊对象。即,它是一个类。
    • 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
    • 目标(Target):被通知对象。
    • 代理(Proxy):向目标对象应用通知之后创建的对象。
    • 切入点(PointCut):切面通知执行的“地点“的定义
    • 连接点(JointPoint):与切入点匹配的执行点

    3.使用Spring实现AOP

    导入依赖包

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.5</version>
    </dependency>
    

    方法一:使用Spring的API接口

    BeforeLog.java

    public class BeforeLog implements MethodBeforeAdvice {
        public void before(Method method, Object[] args, Object target) throws Throwable {
            System.out.println(target.getClass().getName() + "的" + method.getName() + "被执行了");
        }
    }
    

    AfterLog.java

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

    applicationContext.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:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <bean id="userService" class="cn.pinked.service.UserServiceImpl"/>
        <bean id="beforeLog" class="cn.pinked.log.BeforeLog"/>
        <bean id="afterLog" class="cn.pinked.log.AfterLog"/>
    
        <aop:config>
            <aop:pointcut id="pointcut" expression="execution(* cn.pinked.service.UserServiceImpl.*(..))"/>
            <aop:advisor advice-ref="BeforeLog" pointcut-ref="pointcut"/>
            <aop:advisor advice-ref="AfterLog" pointcut-ref="pointcut"/>
        </aop:config>
    </beans>
    

    方法二:使用自定义类实现AOP

    DiyPointCut.java

    public class DiyPointCut {
        public void before(){
            System.out.println("=====方法执行前=====");
        }
        public void after(){
            System.out.println("=====方法执行后=====");
        }
    }
    

    applicationContext.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:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <bean id="userService" class="cn.pinked.service.UserServiceImpl"/>
        <bean id="beforeLog" class="cn.pinked.log.BeforeLog"/>
        <bean id="afterLog" class="cn.pinked.log.AfterLog"/>
        <bean id="diy" class="cn.pinked.diy.DiyPointCut"/>
    
        <aop:config>
            <aop:aspect ref="diy">
                <aop:pointcut id="pointcut" expression="execution(* cn.pinked.service.UserServiceImpl.*(..))"/>
                <aop:before method="before" pointcut-ref="pointcut"/>
                <aop:after method="after" pointcut-ref="pointcut"/>
            </aop:aspect>
        </aop:config>
    </beans>
    

    方法三:使用注解实现AOP

    AnnotationPointCut.java

    @Aspect
    public class AnnotationPointCut {
        @Before("execution(* cn.pinked.service.UserServiceImpl.*(..))")
        public void before(){
            System.out.println("=====方法执行前=====");
        }
        @After("execution(* cn.pinked.service.UserServiceImpl.*(..))")
        public void after(){
            System.out.println("=====方法执行后=====");
        }
    }
    

    applicationContext.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:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <bean id="userService" class="cn.pinked.service.UserServiceImpl"/>
        <bean id="beforeLog" class="cn.pinked.log.BeforeLog"/>
        <bean id="afterLog" class="cn.pinked.log.AfterLog"/>
        <bean id="diy" class="cn.pinked.diy.DiyPointCut"/>
        <bean id="annotation" class="cn.pinked.diy.AnnotationPointCut"/>
        <aop:aspectj-autoproxy/>
    </beans>
    
  • 相关阅读:
    4.24成果(冲刺1.7)
    4.23成果(冲刺1.6)
    4.22成果(冲刺1.5)
    4.21成果(冲刺1.4)
    4.20成果(冲刺1.3)
    4.19成果(冲刺1.2)
    4.18成果(冲刺1.1)
    计划会议
    需求评审
    电梯演讲
  • 原文地址:https://www.cnblogs.com/pinked/p/12207317.html
Copyright © 2011-2022 走看看