服务端第三次课程:面向切面编程AOP

3:面向切面编程AOP

1:回顾

  • bean的组装方式

    • 规划的装配 component autowired sacn是在configuration底下的
    • Java config 使用configuration
      • 函数调用,方法调用(调用加了bean方法的)
      • bean方法的参数,相当于auto wired
    • XML配置
  • profile 可以帮我们干什么?加载方法上面还是类上面很重要的东西

    • 指定要创建那些bean,系统有很多的bean,不同的环境是不一样的
      • 开发,生产,测试
  • conditionlal 条件话的创建注解

    • @bean
    • @conditional
  • @qualifier

  • bean 的作用域

    • 默认是singleton ,一个实例
    • prototype get一个bean 的时候,就给你一个
    • session 多次的交互 ,spring维持一个session的bean
    • request

2:编程方法

  • POP,最小的单元是函数
  • OOP,最小的单元是类
  • AOP,
  • FP,函数式编程:多核技术的发展
  • Rx,反应式编程:

3:AOP

1:引导

  • 日志在

  • 认证:鉴别一个人的身份

  • 授权:登录之后,看有没有权力

    • 你要登机,你需要出示你的身份证和机票,身份证是为了证明你张三确实是你张三,这就是 authentication;而机票是为了证明你张三确实买了票可以上飞机,这就是 authorization。
  • 把我们系统当中的,通用的逻辑抽取出来,与具体的业务逻辑隔离开,运行

2:继承和委托

  • 在音乐会之前需要做一些事情
  • 在音乐会之后需要做一些事情
    • 创建一个对象,对象注入到concert,然后调用观众的方法,进行前后的事情

3:横切关注点

  • 关注的事务

    • 日志
    • 安全
    • 事务
    • 缓存
  • advice 通知

    • 关闭手机 + before
    • 切面做什么,以及什么时候做
  • poincut 切点

    • 在哪里做
    • perform方法
    • 切点表达式 execution(* concert.performance.perform(…))
    • 两个点表示参数,不关心参数是一个还是两个,还是多个
  • 切面

    • 这个类同统称为 一个切面(aspect)

    • 定义方式

      • 注解的方法

        • do not repeat yourself

        • 代码和注释不要重复

          • 难以维护
          package concert;import org.aspectj.lang.annotation.*;@Aspect
          public class Audience1 {@Pointcut("execution(* concert.Performance.perform( .. ))")public void performance() {}@Before("performance()")public void silenceCellPhones() {System.out.println("Silencing cell phones");}
      • XML的方式

  • 连接点

  • 引入

    • 引入新功能
    • introduction
  • 织入

    • 织入时机
    • 编译器,用特殊的语法来写切面,和正常的代码编译在一起
    • 类加载期,加载的时候,
    • 运行期,spring只支持代理

4:通知的类型

  • before
    • 同一个类的aspect,定义了两个相同的advice,定义两个before
    • 那么这两个的执行顺序是无法确定的,哪怕你规定了order数值,也不行
  • after
  • afterreturning
  • afterthrowing
  • around
@EnableAspectJAutoProxy //开启AspectJ的自动代理机制
  • 我拿到的时候一个proxy的引用,在适当的实际调用

5:切点指示器

  • with 是包路径的限定,只在这些包地下
&& within(soundsystem.*) && args(trackNumber)"   
  • bean 针对某一个具体的bean,来实施制图

4:问题

  • 我如果要织入多个,这个实例到底是一个多实例,还是一个多实例
  • defaultEncoreable这个类我没有实例化,当我现在实现多个接口的对象,如果有三个,那么有三个都会增加新的行
    • 这个defaultEncoreable 是单实例还是???

3:XML


<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/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><aop:aspectj-autoproxy/><bean id="audience" class="concert2.Audience"/><bean id="concert" class="concert.Concert"/><aop:config><aop:aspect ref="audience"><aop:before method="silenceCellPhones"pointcut="execution(* concert.Performance.perform(..))"/><aop:before method="takeSeats"pointcut="execution(* concert.Performance.perform(..))"/><aop:after method="applause"pointcut="execution(* concert.Performance.perform(..))"/><aop:after-throwing method="demandRefund"pointcut="execution(* concert.Performance.perform(..))"/>aop:aspect>aop:config>
beans>
注入的时候,没有这个属性  委托这个属性
我们可以自己实例化,然后给引用


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部