一、面向切面的编程需求的产生

    1. 代码混乱:越来越多的非业务需求(日志和验证等)加入后,原有的业务方法急剧膨胀。每个方法在处理核心逻辑的同时还必须兼顾其他多个关注点。
    2. 代码分散: 以日志需求为例,只是为了满足这个单一需求,就不得不在多个模块(方法)里多次重复相同的日志代码。如果日志需求发生变化,必须修改所有模块。

  二、实现面向切面的编程

    1. 将需要实现AOP的类注入到Spring容器中,例如:
       1 package com.neuedu.aop;
       2 
       3 import org.springframework.stereotype.Component;
       4 
       5 @Component
       6 public class RawCaculator implements MathCaculator{
       7 
       8     @Override
       9     public int add(int i, int j) {
      10         int rs=i+j;
      11         System.out.println(i+"+"+j+"="+rs);
      12         return rs;
      13     }
      14 
      15     @Override
      16     public int sub(int i, int j) {
      17         int rs=i-j;
      18         System.out.println(i+"-"+j+"="+rs);
      19         return rs;
      20     }
      21 
      22     @Override
      23     public int mul(int i, int j) {
      24         int rs=i*j;
      25         System.out.println(i+"*"+j+"="+rs);
      26         return rs;
      27     }
      28 
      29     @Override
      30     public int div(int i, int j) {
      31         int rs=i/j;
      32         System.out.println(i+"/"+j+"="+rs);
      33         return rs;
      34     }
      35 
      36 }
      要实现AOP的计算方法类

相关文章:

  • 2021-11-23
  • 2018-10-06
  • 2021-07-01
  • 2021-11-28
  • 2021-11-14
猜你喜欢
  • 2021-10-11
  • 2021-11-23
  • 2021-06-29
  • 2022-12-23
  • 2021-08-23
  • 2021-07-20
  • 2021-07-12
相关资源
相似解决方案