什么是AOP

一,why  AOP?

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

总结: 把核心业务代码和非核心业务代码进行分离,从而降低核心业务代码和非核心代码的耦合度。

举例说明:使用动态代理可以完成核心业务代码和非核心业务代码的分离

1.创建接口

public interface ArithmeticCalculator {

    /**
     * 加法运算
     * @param a
     * @param b
     * @return
     */
    public int add(int a,int b);

    /**
     * 减法运算
     * @param a
     * @param b
     * @return
     */
    public int sub(int a,int b);

    /**
     * 乘法运算
     * @param a
     * @param b
     * @return
     */
    public int mul(int a,int b);

    /**
     * 除法运算
     * @param a
     * @param b
     * @return
     */
    public int div(int a,int b);
}

2.接口的实现类

public class ArithmeticCalculatorImpl implements ArithmeticCalculator{

    public int add(int a, int b) {
        int result=a+b;
        return result;
    }

    public int sub(int a, int b) {
        int result=a-b;
        return result;
    }

    public int mul(int a, int b) {
        int result=a*b;
        return result;
    }

    public int div(int a, int b) {
        int result=a/b;
        return result;
    }
}

3.创建代理类

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class LogProxy {//日志代理类

     private ArithmeticCalculator target;//被代理的对象
     public LogProxy(ArithmeticCalculator target){
          this.target=target;
     }

     public ArithmeticCalculator getProxyInstance(){

         //ClassLoader loader,
         //Class<?>[] interfaces,
         //InvocationHandler h
         ClassLoader loader=target.getClass().getClassLoader();
         Class<?>[] interfaces={ArithmeticCalculator.class};   //被代理的类实现了哪些接口
         InvocationHandler h=new InvocationHandler() {
             public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                 System.out.println("The method "+method.getName()+" beigin with:"+Arrays.asList(args));
                 Object result = method.invoke(target, args);
                 System.out.println("The method "+method.getName()+" end with:"+result);
                 return result;
             }
         };
         ArithmeticCalculator instance =(ArithmeticCalculator) Proxy.newProxyInstance(loader,interfaces,h);//获取代理类对象
         return instance;
     }

}

4.测试

public class Test {
    public static void main(String[] args) {
          //创建被代理的对象。---张三
          ArithmeticCalculator target=new ArithmeticCalculatorImpl();
          //获取代理对象----李四
         LogProxy logProxy=new LogProxy(target);
         ArithmeticCalculator proxy=logProxy.getProxyInstance();
         //使用代理对象调用相应的方法
        int add = proxy.add(10, 5);
        System.out.println("结果:"+add);

        int sub = proxy.sub(10, 5);
        System.out.println("结果:"+sub);
    }
}

如上所示,这种写法很难理解?  没关系  spring框架帮你抽取了aop模式

使用AOP的步骤

(1)引入依赖--jar包   该版本号需要和spring其他框架版本号一致

<!--引入切面依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

(2)创建一个日志切面

/**
 * aop:得使用步骤:
 *    (1)引入springaspect依赖
 *    (2)创建一个日志切面类。 该类必须使用@Aspect注解来标记。
 *    (3)该类也必须有spring容器来管理。@Component
 *    (4)spring配置文件中必须开启Aspect注解<aop:aspectj-autoproxy/>
 */
@Component  //@Service @Controller一样
@Aspect  //表示该类为切面类
public class LogAspect {
    /**
     *       第一个* 表示该方法得修饰符和返回类型
     *       第二个* 表示指定包下所有的类。
     *       第三个* 表示该类下所有的方法
     *       .. 方法得任意参数
     */


    //该方法在指定表达式之前执行
    @Before(value = "execution(* com.lj.aop.after.*.*(..))") 
    public void before(){
        System.out.println("日志开始");
    }


    //该方法在指定表达式之后执行
    @After(value = "execution(* com.lj.aop.after.*.*(..))")
    public void after(){
        System.out.println("日志结束");
    }

}

(3)spring.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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--开启包扫描-->
    <context:component-scan base-package="com.ykq.aop.after"/>

    <!--开始切面注解-->
    <aop:aspectj-autoproxy/>
</beans>

(4)测试

public class Test {
    public static void main(String[] args) {
        ApplicationContext app=new ClassPathXmlApplicationContext("spring.xml");
        ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) 			               app.getBean("arithmeticCalculator");
        double add = arithmeticCalculator.fun(10, 5,6);
        System.out.println("结果:"+add);
    }
}

以上是基于注解开启切面编程

还有一种基于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:tx="http://www.springframework.org/schema/tx"
       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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--开启包扫描-->
    <context:component-scan base-package="com.lj.service"/>
  
    


    <!--事务管理  理解为事务切面-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datasoure"/>
    </bean>

    <!--配置事务的属性 必须规范-->
    <!--read-only意思是只读   表示该名下的方法为只读  不能修改  一般用在查询方法上-->

    <tx:advice id="myadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="select*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED" timeout="3"/>
        </tx:attributes>
    </tx:advice>
    <!--配置切面-->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.lj.service.*.*(..))"/>
        <aop:advisor advice-ref="myadvice" pointcut-ref="pointcut"/>
    </aop:config>


</beans>

二,AOP常用注解

@Before: 前置通知, 在方法执行之前执行

@After: 后置通知, 在方法执行之后执行

@AfterRunning: 返回通知, 在方法返回结果之后执行

@AfterThrowing: 异常通知, 在方法抛出异常之后

总结:

1.AOP: 什么是AOP?

切面编程,将核心业务代码与非核心业务代码分离,降低耦合性

2.AOP在实际项目的应用:

事物 日志

3.AOP的实现原理:

动态代理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值