概念:AOP(Aspect-Oriented Programming)即面向切面编程。它是对传统的OOP(面向对象)编程的一种补充,在OOP中往往一个对象有什么行为我们就定义什么方法,对象与对象之间存在紧密联系。与OOP不同的是AOP更加关注的是切面,我们只需要关注于对象的核心业务而不是所有的业务。

如同上图1中所示,两个螺丝就是一种紧密耦合的关系, 一旦一方存在问题,另一方也必须做出相应调整。而图2为一个笔记本的USB插口,只要符合这个接口的设备都可以使用这个插口。

在程序中也和现实生活一样,使用spring AOP就是一种典型的非耦合案例,AOP的核心之一就是我们的非核心业务与我们的核心业务解耦。

具体实现:(本例我们模拟一个女孩子的日常生活,去KFC,约会,遛狗几个事务)

第一步:既然是使用spring自然要配置相关环境,然后直接写我们的核心业务,而在一个程序中我们在多个流程中反复出现的一些非核心代码抽取出来由代理给我们管理。我们发现这几个事务之前我们的对象(女孩子)都要洗澡,化妆......之后都要卸妆....这些非逻辑业务便是我们需要分离出来的。

 package aop_demo;

 /*
*
*/
public class Girl1 implements Girl{ private Dog dog = null; public void setDog(Dog dog) {
this.dog = dog;
} public void KFC(String datetime){ System.out.println("[核心业务逻辑]我是第一个女孩");
System.out.println("[核心业务逻辑]"+datetime+"吃肯德基");
} public void meet(String datetime){ System.out.println("[核心业务逻辑]我是第一个女孩");
System.out.println("[核心业务逻辑]"+datetime+"约会"); } @Override
public void playDog() { dog.bark(); } }

本例中的对象还一个dog属性因此我们还一个dog对象以及它的方法

 package aop_demo;

 public class Dog_taidi implements Dog {

     @Override
public void bark() {
System.out.println("旺旺,我是泰迪");
} }

第二步,统一管理我们的非核心业务也就是代理了。

上图中的通知其本质也就是代理,由于本例中前后都有非核心业务,因此我们选择环绕通知

 package aop_demo;

 import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; public class AroundAdvice implements MethodInterceptor { @Override
public Object invoke(MethodInvocation invocation) throws Throwable { //前置业务
System.out.println("[代理执行前置]洗澡");
System.out.println("[代理执行前置]化妆");
System.out.println("[代理执行前置]穿衣服");
System.out.println("*****************"); //核心业务
Object result = invocation.proceed(); //后置业务
System.out.println("*****************");
System.out.println("[代理执行后置]卸妆");
System.out.println("[代理执行后置]洗澡");
System.out.println("[代理执行后置]听歌");
System.out.println(""); return result;
} }

第三步,配置applicationContext.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
<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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <bean id="girl" class="aop_demo.Girl1" scope="prototype">
<!-- g1.setDog(dog1); -->
<property name="dog" ref="dog1"></property>
</bean> <!-- Dog dog1 = new Dog_taidi(); -->
<bean id="dog1" class="aop_demo.Dog_taidi" scope="prototype"> </bean> <bean id="girlHandler" class="aop_demo.GirlHandler" scope="prototype">
<property name="target" ref="girl"></property>
</bean> <bean id="aroundAdvice" class="aop_demo.AroundAdvice"></bean> <bean id="girlProxy" class="org.springframework.aop.framework.ProxyFactoryBean" scope="prototype">
<property name="interceptorNames" value="aroundAdvice"></property>
<property name="target" ref="girl"></property>
</bean> </beans>

第四步,构造我们的测试程序

 package aop_demo;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml"); Girl g1 = (Girl)context.getBean("girlProxy"); System.out.println("");
System.out.println("");
System.out.println("");
System.out.println(""); g1.KFC("周日");
g1.meet("周六");
//开始遛狗
g1.playDog(); } }

深入理解springAOP的更多相关文章

  1. 如何理解springaop

    初看aop,上来就是一大堆术语,而且还有个拉风的名字,面向切面编程,都说是oop的一种有益补充等等,一下子让你不知所措,心想着:怪不得很多人都和我说aop多难多难.当我看进去以后,我才发现:它就是一些 ...

  2. 深入理解SpringAOP之代理对象

    本篇文章主要带大家简单分析一下AOP的代理对象,至于AOP是什么,如何配置等基础性知识,不在这里讨论.阅读前请先参考:代理模式,在这之前我们需要了解springframework的三个核心接口与get ...

  3. 深入理解springAOP切面的特性

    一张图说明情况

  4. ssm知识点整理

    第1章 resultType和resultMap的区别是什么?  MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType ...

  5. 【设计模式】Java设计模式 - 单例模式

    [设计模式]Java设计模式 - 单例模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 分享学习心得,欢迎指正,大家一起学习成长! 原创作品,更多关注我CSDN: ...

  6. 【设计模式】Java设计模式 - 原型模式

    [设计模式]Java设计模式 - 原型模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 原创作品,更多关注我CSDN: 一个有梦有戏的人 准备将博客园.CSDN一起 ...

  7. 【设计模式】Java设计模式 - 适配器模式

    [设计模式]Java设计模式 - 适配器模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 原创作品,更多关注我CSDN: 一个有梦有戏的人 准备将博客园.CSDN一 ...

  8. 【设计模式】Java设计模式 - 桥接模式

    [设计模式]Java设计模式 - 桥接模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 原创作品,更多关注我CSDN: 一个有梦有戏的人 准备将博客园.CSDN一起 ...

  9. 【SpringAop】【统一日志处理】注解方式理解以及使用

    [注意:本次代码的demo会存在百度网盘,由于公司的保密,禁止上传,所以仅本人可见] 目前公司在做数据资产项目,数据质量部分使用到了springaop做统一日志处理,以前对这块有了解,有点模糊不清,今 ...

随机推荐

  1. DDD实施经验分享—价值导向、从上往下进行(圈内第一个吃螃蟹DDD实施方案)

    阅读目录: 1.背景 2.从业务开始 3.从战略到战术 4.借助外力推动研发(QA.领导.自动化测试) 5.领域模型与SAAS平台的内核(价值最大化) 6.最后 1.背景 DDD本身的技术就不介绍了, ...

  2. 安装mysql后的基本配置

    1.添加环境变量 右键 此电脑->属性->高级系统设置->环境变量,在系统变量里面找到Path,双击.点击编辑,将mysql中bin文件的路径添加到最后一行,如:F:\AppSev\ ...

  3. 【hive】——Hive基本操作

    阅读本文章可以带着下面问题:1.与传统数据库对比,找出他们的区别2.熟练写出增删改查(面试必备) 创建表:hive> CREATE TABLE pokes (foo INT, bar STRIN ...

  4. SpringMVC 处理异常的4种方式

    springmvc处理异常有三种方式: 1.在一个controller中定义一个方法,用@ExceptionHandler注解标注.(优先级最高) @ExceptionHandler public M ...

  5. 【转】document.cookie详解

    设置cookie 每个cookie都是一个名/值对,可以把下面这样一个字符串赋值给document.cookie: document.cookie="userId=828"; 如果 ...

  6. linux 环境下安装mysql5.6

    在网上找了很多博客 看着头晕眼花 各个步骤 最终功夫不负有心人 终于安装好了 特此整理分享一下 1> #yum remove mysql mysql-*    //卸载原先版本的mysql 2& ...

  7. mybatis配置-返回date类型丢失时间

    此博客仅作于平时开发所遇到的问题记录,不做他用,描述可能不好,自己看懂即可~~ resultMap配置返回时间类型时,发现数据库时间是精确到秒的,但是返回给javabean之后丢失时分秒的信息,只有日 ...

  8. 集群(cluster)和高可用性(HA)的概念

    1.1 什么是集群 简单的说,集群(cluster)就是一组计算机,它们作为一个整体向用户提供一组网络资源.这些单个的计算机系统就是集群的节点(node).一个理想的集群是,用户从来不会意识到集群系统 ...

  9. Linux FHS

    学习linux有必要了解FHS的知识,FHS英文全称是Filesystem Hierarchy Standard.中文翻译就是文件系统层次结构标准.关于这个详细的介绍参考官方的文档和鸟哥linux书. ...

  10. org.hibernate.HibernateException: No Session found for current thread

    spring.springmvc和hibernate整合 在sessionFactory.getCurrentSession()时,出现以下异常 No Session found for curren ...