亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Spring Aop

標簽:
Spring

AOP(Aspect-OrientedProgramming,面向切面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善。OOP引入封装、继承和多态性等概念来建立一种对象层次结构,用以模拟公共行为的一个集合。当我们需要为分散的对象引入公共行为的时候,OOP则显得无能为力。也就是说,OOP允许你定义从上到下的关系,但并不适合定义从左到右的关系。例如日志功能。日志代码往往水平地散布在所有对象层次中,而与它所散布到的对象的核心功能毫无关系。对于其他类型的代码,如安全性、异常处理和透明的持续性也是如此。这种散布在各处的无关的代码被称为横切(cross-cutting)代码,在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用.
而AOP则能够将这种散布在各处的无关代码抽象出来,减少代码重复。
AOP使用场景:
Authentication 权限

Caching 缓存

Context passing 内容传递

Error handling 错误处理

Lazy loading 懒加载

Debugging  调试

logging, tracing, profiling and monitoring 记录跟踪 优化 校准

Performance optimization 性能优化

Persistence  持久化

Resource pooling 资源池

Synchronization 同步

Transactions 事务

主要介绍两种AOP实现方式:纯POJO方式和注解配置方式

纯POJO方式

<!--AOP配置-->
    <aop:config proxy-target-class="true">
        <aop:aspect id="mywatch" ref="watch">
            <aop:pointcut id="display" expression="execution(* com.biz.StudentServiceImpl.display())" />
            <aop:before method="before" pointcut-ref="display" />
            <aop:after method="after" pointcut-ref="display" />
        </aop:aspect>
        <aop:aspect id="mywatch1" ref="watch">
            <aop:pointcut id="save" expression="execution(* com.biz.StudentServiceImpl.save())" />
            <aop:after-returning method="returning" pointcut-ref="save" returning="result"/>
        </aop:aspect>
    </aop:config>
package com.biz;/**
 * Created by Tired on 2018/4/3.
 */public class StudentServiceImpl implements ISerivce {    public void save() {
        System.out.println("正在执行新增操作。。。。。。");
    }    public void display() {
        System.out.println("正在展示学生信息");
    }
}
 public static void aop() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentServiceImpl bll = (StudentServiceImpl) context.getBean("stuService");
       bll.display();
        bll.save();
    }

注解方式

package com.aop;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.springframework.stereotype.Component;/**
 * Created by Tired on 2018/4/3.
 */@Aspect@Componentpublic class HelloWorldAspect {    @Pointcut(value = "execution(* com.biz.StudentServiceImpl.save())")    public void pointCut (){

    }    @Before(value = "pointCut ()")    private void beforeAdvice (){
        System.out.println("===========before advice param:");
    }    @After( value = "pointCut ()")    private void afterAdvice (){
        System.out.println("===========after advice param:");
    }    @Around(value = "pointCut ()")    private void aroundAdvice (ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("===========before advice param: around");
        pjp.proceed();
        System.out.println("===========after advice param: around");
    }
}



作者:TiredHu
链接:https://www.jianshu.com/p/6ffa352c8f5f


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
全棧工程師
手記
粉絲
233
獲贊與收藏
1006

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消