1.AOP:aspect orientied programming 面向切面编程。就是横向编程。

2.面向切面编程是在不改变原有代码的情况下增加新的功能。

3.在spring中面向切面编程有两种应用:

  a) 声明式事务

  b) 自定义aop编程

4.spring 提供了aop的相关概念及api :

切面(Aspect) : 一个关注点的模块化,这个关注点可能会横切多个对象。

连接点 (Joinpoint) : 在程序执行过程中某个特定的点,在Spring AOP 中,一个连接点总是表示一个方法的执行。

通知 (Advice) : 在切面的某个特定的连接点上执行的动作。

切入点( Pointcut) : 匹配连接点的断言。

目标对象(Target Object): 被一个或多个切面所通知的对象。

AOP 代理(AOP Proxy):AOP代理可以是JDK动态代理或者CGLIB代理。

织入(Weaving) : 把切面连接到其他应用程序类型或者对象上,并创建一个被通知的对象。

5.通知的类型

  a) 前置通知(Before advice) : 在某连接点之前执行的通知,但这个通知不能阻止连接点之前的执行流程(除非它抛出一个异常)。

  b) 后置通知(After returning advice): 在某连接点正常完成后执行的通知;例如,一个方法没有抛出任何异常,正常返回。

  c) 异常通知(After throwing advice) : 在方法抛出异常退出时执行的通知(不论正常返回还是异常退出)。

  d) 环绕通知(Around Advice): 包围一个连接点的通知,如方法调用。

6.使用spring提供的api进行 aop 的开发

  a) 新建 java 项目

  b) 导入 jar 包

aopalliance.jar

aspectjweaver.jar

commons-logging.jar

spring-aop-4.1.6.RELEASE.jar

spring-aspects-4.1.6.RELEASE.jar

spring-beans-4.1.6.RELEASE.jar

spring-context-4.1.6.RELEASE.jar

spring-core-4.1.6.RELEASE.jar

spring-expression-4.1.6.RELEASE.jar

spring-tx-4.1.6.RELEASE.jar

  c) 编写相关类

LogAdvice.java

/**
*
* 实现一个公共的日志业务,该业务实现了spring提供的前置通知
*/
public class LogAdvice implements MethodBeforeAdvice{ @Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getSimpleName()+":"+method.getName()+"方法被执行");
} }

UserServiceImpl.java

/**
* 在service的方法中,经常会有一些公共的功能,
* 如:事务,日志,权限,缓存等
*
*/
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("add");
}
@Override
public void delete() {
System.out.println("delete");
}
@Override
public void update() {
System.out.println("update");
}
}

  d) 编写配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 这是公共业务 -->
<bean id="logAdvice" class="cn.sxt.aop.LogAdvice"/>
<!-- 这是真实对象 -->
<bean id="userService" class="cn.sxt.service.impl.UserServiceImpl"/>
<!-- 使用spring的aop配置 将公共业务和真实对象联系起来
以前这个动作需要通过实现动态代理来完成,spring将动态代理
封装好了,只需要通过指定配置来完成即可
-->
<aop:config>
<aop:pointcut expression="execution(* cn.sxt.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="logAdvice" pointcut-ref="pointcut"/>
</aop:config>
</beans>

  e) 测试

public class AopTest {
@Test
public void testBeforeAdvice(){
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
UserService us = (UserService)ac.getBean("userService");
us.add();
}
}

7. spring提供了一种 无侵入性 的 aop 实现

通知的代码

/**
*
* 实现一个公共的日志业务
*/
public class LogAdvice{
public void log(){
System.out.println("-----相关日志信息----");
}
}

配置

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 这是公共业务 -->
<bean id="logAdvice" class="cn.sxt.aop.LogAdvice"/>
<!-- 这是真实对象 -->
<bean id="userService" class="cn.sxt.service.impl.UserServiceImpl"/>
<!-- 使用spring的aop配置 将公共业务和真实对象联系起来
以前这个动作需要通过实现动态代理来完成,spring将动态代理
封装好了,只需要通过指定配置来完成即可
-->
<aop:config>
<aop:pointcut expression="execution(* cn.sxt.service.impl.*.*(..))" id="pointcut"/>
<aop:aspect ref="logAdvice">
<!-- 配置前置通知 -->
<aop:before method="log" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>

8. 通过注解来实现 aop

通知

/**
*
* 实现一个公共的日志业务
*/
@Aspect
public class LogAdvice{
@Before("execution(* cn.sxt.service.impl.*.*(..))")
public void log(){
System.out.println("-----相关日志信息----");
}
}

配置中,使用自动配置

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 这是公共业务 -->
<bean id="logAdvice" class="cn.sxt.aop.LogAdvice"/>
<!-- 这是真实对象 -->
<bean id="userService" class="cn.sxt.service.impl.UserServiceImpl"/>
<!-- 使用注解来实现aop -->
<aop:aspectj-autoproxy/>
</beans>

java之aop的更多相关文章

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

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

  2. 从零开始学 Java - Spring AOP 实现主从读写分离

    深刻讨论为什么要读写分离? 为了服务器承载更多的用户?提升了网站的响应速度?分摊数据库服务器的压力?就是为了双机热备又不想浪费备份服务器?上面这些回答,我认为都不是错误的,但也都不是完全正确的.「读写 ...

  3. Java Spring AOP用法

    Java Spring AOP用法 Spring AOP Java web 环境搭建 Java web 项目搭建 Java Spring IOC用法 spring提供了两个核心功能,一个是IoC(控制 ...

  4. JAVA平台AOP技术研究

    3.1 Java平台AOP技术概览 3.1.1 AOP技术在Java平台中的应用 AOP在实验室应用和商业应用上,Java平台始终走在前面.从最初也是目前最成熟的AOP工具--AspectJ,到目前已 ...

  5. Java 中 AOP —— 探讨其存在价值及实现方式对比

    AOP 概述 Aspect-oriented programming(面向切面编程).最广为人知的面向侧面的程序设计语言是由施乐帕洛阿尔托研究中心 (施乐帕克 nb!)开发的AspectJ,该语言可以 ...

  6. 【转】Java Spring AOP详解

    一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...

  7. Java Objective-C AOP

    Java Use an AOP library or byte-code engineering (BCEL, cglib, asm, etc) to create a sub-class on th ...

  8. java之aop使用及自定义注解

    目的: 1.Java注解简介 2.Java元注解(重点) 3.自定义注解 案例一(获取类与方法上的注解值) 案例二(获取类属性上的注解属性值) 案例三(获取参数修饰注解对应的属性值) 4.Aop自定义 ...

  9. [刘阳Java]_Spring AOP基于XML配置介绍_第9讲

    基于注解配置的Spring AOP固然简单,但是这节我们会给大家介绍基于XML配置的AOP是如何应用的.为什么这么说了,因为后面我们还会介绍到Spring对Dao操作的事务管理(基于AOP的XML文件 ...

随机推荐

  1. ubuntu之路——day19.2 开源框架与迁移、CNN中的数据扩充

    开源框架与迁移 上面介绍了一些已经取得很好成绩的CNN框架,我们可以直接从GitHub上下载这些神经网络的结构和已经在ImageNet等数据集上训练好的权重超参数. 在应用于我们自己的数据时. 1.如 ...

  2. [Beta阶段]第六次Scrum Meeting

    Scrum Meeting博客目录 [Beta阶段]第六次Scrum Meeting 基本信息 名称 时间 地点 时长 第六次Scrum Meeting 19/05/12 大运村寝室6楼 25min ...

  3. [BUAA软工]Beta阶段测试报告

    Beta阶段测试报告 Bug发现与报告 BUG 出现原因 解决方案 将shell加上编辑器UI以后,两边显示的文件不同步 两边的根目录不一致 修改编辑器获取根目录的函数,使其与shell的/home目 ...

  4. 每天一个命令-cp 命令

    cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录.它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下.cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文 ...

  5. Mysql:设置主键自动增长起始值

    比较郁闷昨天在家使用‘alter table `tablename` AUTO_INCREMENT=10000;’怎么也不起效,但是今天下班时间公司一同事尝试了一下就可以了.搞不明白自己当时是怎么操作 ...

  6. PHP Y2K38 (2038年) 问题

    PHP 的 strtotime('2100-01-01'); 转换失败:经查询是因为32位系统的 Y2K38问题: Y2K38 问题:当时间大于 2038年01月19日03:14:07 时,strto ...

  7. python2与python3的区别齐全【整理】

    本文链接:https://blog.csdn.net/pangzhaowen/article/details/80650478 展开 一.核心类差异1. Python3 对 Unicode 字符的原生 ...

  8. PMP 第4章错题总结

    变更步骤: 1.配置管理活动:配置识别.配置状态记录.配置核实与审计2.项目章程中记录项目的目的和总体预算3.变更控制系统规定了变更管理流程及批准的权限4.项目章程是授权项目经理动用组织资源的文件5. ...

  9. Spring Shell入门介绍

    目录 Spring Shell是什么 入门实践 基础配置 简单示例 注解@ShellMethod 注解@ShellOption 自定义参数名称 设置参数默认值 为一个参数传递多个值 对布尔参数的特殊处 ...

  10. maven工程仿springboot手写代码区分开发测试生产

    读取代码: package com.jz.compute.mc.v2.config; import java.util.Enumeration; import java.util.ResourceBu ...