一、什么是AOP

  AOP(Aspect-Oriented Programming,面向切面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善。通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

二、为什么需要AOP 

  1.分离系统中的各种关注点,将核心关注点和横切关注点分离开来。

  2.减少代码的重复,各个模块的重用性加强。

  3.降低 模块间的耦合度,提高代码的可操作性和可维护性。

三、AOP原理

  将复杂的需求分解出不同方面,将散布在系统中的公共功能集中解决

  采用代理机制组装起来运行,在不改变原程序的基础上对代码段进行增强处理,增加新的功能。

  

四、AOP相关术语

   1 增强处理类型

    前置增强:目标方法调用前

    后置增强:目标方法调用后

    环绕增强:前置+后置

    异常抛出增强:只有在目标方法抛出异常时才执行

    最终增强:finally

 

  2 AOP设计单元

五、AOP案例

  1.创建实体类

    略;

  2.创建接口

    

public interface IUserMapper {
    public int addUser(IUser iUser);
}

  3.创建实现类

public class IUserMapperImpl implements IUserMapper {

    @Override
    public int addUser(IUser iUser) {
        System.out.println("add success");
        return 0;
    }
}

  4.创建切面类

public class MyAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("前置增强");
    }
}

  5.编写applicationContext.xml配置文件

<!--声明MapperBean-->
    <bean id="iuserMapper" class="cn.spring.mapper.impl.IUserMapperImpl"></bean>
   <!--声明serviceBean-->
    <bean id="iuserService" class="cn.spring.service.impl.IUserServiceImpl">
        <property name="iUserMapper" ref="iuserMapper"></property>
    </bean>

    <!--切面:增强类-->
    <bean id="myAdvice" class="cn.spring.advice.MyAdvice"></bean>
    <!--增强处理-->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* *..service.*.*(..))"/>
        <!--织入:将增强处理和切点表达式符合的方法关联到一起-->
        <aop:advisor advice-ref="myAdvice" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>

  6.编写测试类

 @Test
    public void test1(){
        ApplicationContext contet=new ClassPathXmlApplicationContext("applicationContext.xml");
        IUserService iuserService = (IUserService) contet.getBean("iuserService");
        iuserService.addUser(new IUser());
    }

五、IOC与AOP交互扩展

   1 使用多种方式实现 IOC

    1.1 构造注入
      

 public Student(Integer stu_id, String stu_name) {
        this.stu_id = stu_id;
        this.stu_name = stu_name;
    }

    public Student(){

    }
 <!--基于构造注入-->
    <bean id="student" class="cn.spring.student.Student">
        <constructor-arg type="java.lang.Integer" value="15"></constructor-arg>
        <constructor-arg type="java.lang.String"  value="李四"></constructor-arg>
    </bean>

    1.2 p命名空间注入

xmlns:p="http://www.springframework.org/schema/p"
public class Student {
    private Integer stu_id;
    private String stu_name;

    public Integer getStu_id() {
        return stu_id;
    }

    public void setStu_id(Integer stu_id) {
        this.stu_id = stu_id;
    }

    public String getStu_name() {
        return stu_name;
    }

    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }

}
<!--p命名空间注入-->
    <bean id="student" class="cn.spring.student.Student" p:stu_id="78" p:stu_name="王五"></bean>

Spring AOP的使用及案例的更多相关文章

  1. 关于 Spring AOP (AspectJ) 该知晓的一切

    关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上 ...

  2. Spring AOP初步总结(二)

    该篇为Spring AOP的一个应用案例:系统日志 需求:将任何删除,更改或新增数据库的操作汇总到数据库中 步骤1:编写切面 @Aspect @Component public class SysLo ...

  3. 关于 Spring AOP (AspectJ) 你该知晓的一切

    版权声明:本文为CSDN博主「zejian_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/javazej ...

  4. 161220、使用Spring AOP实现MySQL数据库读写分离案例分析

    一.前言 分布式环境下数据库的读写分离策略是解决数据库读写性能瓶颈的一个关键解决方案,更是最大限度了提高了应用中读取 (Read)数据的速度和并发量. 在进行数据库读写分离的时候,我们首先要进行数据库 ...

  5. 170301、使用Spring AOP实现MySQL数据库读写分离案例分析

    使用Spring AOP实现MySQL数据库读写分离案例分析 原创 2016-12-29 徐刘根 Java后端技术 一.前言 分布式环境下数据库的读写分离策略是解决数据库读写性能瓶颈的一个关键解决方案 ...

  6. 使用Spring AOP实现MySQL数据库读写分离案例分析

    一.前言 分布式环境下数据库的读写分离策略是解决数据库读写性能瓶颈的一个关键解决方案,更是最大限度了提高了应用中读取 (Read)数据的速度和并发量. 在进行数据库读写分离的时候,我们首先要进行数据库 ...

  7. 从零开始学 Java - Spring AOP 实现用户权限验证

    每个项目都会有权限管理系统 无论你是一个简单的企业站,还是一个复杂到爆的平台级项目,都会涉及到用户登录.权限管理这些必不可少的业务逻辑.有人说,企业站需要什么权限管理阿?那行吧,你那可能叫静态页面,就 ...

  8. 基于 Annotation 拦截的 Spring AOP 权限验证方法

    基于 Annotation 拦截的 Spring AOP 权限验证方法 转自:http://www.ibm.com/developerworks/cn/java/j-lo-springaopfilte ...

  9. [Spring框架]Spring AOP基础入门总结一.

    前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...

随机推荐

  1. Windows下更换MAC地址

    使用TMAC软件是最佳方案.官网地址:www.technitium.com

  2. Junit测试Service类方法教程

    Junit测试是很方便的,本博客记录一下Junit测试一些Service接口的方法,这样可以不运行项目,在@Test注解的方法里直接测试 Maven引入jar包: <properties> ...

  3. web-文件上传漏洞总结

    思维导图: 一,js验证绕过 1.我们直接删除代码中onsubmit事件中关于文件上传时验证上传文件的相关代码即可. 或者可以不加载所有js,还可以将html源码copy一份到本地,然后对相应代码进行 ...

  4. loadrunner12下载、安装、认证、汉化

    友情提示 推荐工具pandownload下载 本文尽可能地写得详细一些,有不懂的请先自行百度 安装过程中会有大量英文,可以用有道词典截图翻译 若你的电脑只有一个分区,则建议所有位置选择默认,或者根据个 ...

  5. Spring boot 梳理 - WebMvcConfigurer接口 使用案例

    转:https://yq.aliyun.com/articles/617307 SpringBoot 确实为我们做了很多事情, 但有时候我们想要自己定义一些Handler,Interceptor,Vi ...

  6. Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)

    Creating the POM <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...

  7. There is a cycle in the hierarchy解决

    前言: 在一次项目中,分页查询公告列表信息后,在遍历查询到的公告列表时出现了死循环“There is a cycle in the hierarchy”错误,分析原因是因为在公告实体类中包含了商铺对象 ...

  8. Flask基础(06)-->视图常用逻辑

    Flask基础(06)-->视图常用逻辑 返回json 重定向:url_for 自定义状态码 返回json:在使用 Flask 写一个接口时候需要给客户端返回 JSON 数据,在 Flask 中 ...

  9. MongoDB入门系列之科普篇

    ​ 目录 背景 对比 MongoDB的数据存储格式 背景 最近公司扩展了很多国外客户,那么一个很严重的问题就是翻译,对于国外客户来说,肯定看不懂中文,那就要项目中提供切换各自国家语言的功能. 由于每个 ...

  10. phpexcel来做表格导出(多个工作sheet)及设置单元格格式

    <?php /** * 简单实用Execl */ set_include_path('.'.get_include_path().PATH_SEPARATOR.dirname(__FILE__) ...