为了账号安全,请及时绑定邮箱和手机立即绑定

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
获赞与收藏
1008

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消