AOP面向切面编程

概念:方便我们将一些非核心业务逻辑抽离,从而实现核心业务和非业务分解耦,比如添加一个商品信息,那么核心业务就是做添加商品信息记录这个操作,非核心业务比如:事务的管理,日志,全局异常,性能检测,读写分离的实现等等。。。

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

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

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

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

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

AOP在实际项目的应用:事务 日志

AOP的实现原理:动态代理

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

接口类:

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);
}


package com.ykq.aop.proxy;

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;
    }
}

代理类:

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;
     }

}

测试:

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步骤:

①引入依赖

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

②创建一个日志切面类

/**
*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.ykq.aop.after.*.*(..))") //该方法在指定表达式之前执行
    public void before(){
        System.out.println("日志开始");
    }
    @After(value = "execution(* com.ykq.aop.after.*.*(..))")
    public void after(){
        System.out.println("日志结束");
    }


}

③开启切面注解

<?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>

④测试

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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值