接上一篇:[原创]spring及springmvc精简版--IOC

理解AOP。java是一种面向对象的语言。而AOP是面向切面,在我看来是面向逻辑或者业务编程,它是对一组逻辑的抽象和分配

经典例子,很多系统都有日志。以登录为例子。常规编程流程大致如下:点击登录--->写入日志--->后台处理--->写入日志。因为我们的系统中会有很多功能逻辑代码都是如此去处理日志。假设有一天,需求改变不需要日志了。那么我们如何去处理这些已经存在于整体逻辑单元中的日志代码?无非是找到每一个使用日志的地方,逐一删除。大家可想这样的效率?代码的耦合度?

而AOP变成。就是为了高度的解耦儿产生。它将登录的整个流程进行按照特定的,共同的业务逻辑进行切割,从而抽象出来了一组公共的逻辑单元。然后在根据不同业务模块的需求,在某些业务指定的地方将公共的业务逻辑植入其中,从而形成了一个整体的业务逻辑单元,实现某一模块功能。(这些是自己思考,总结的,刚开始接触的时候,没有理解到这点,也吃了很多闭门羹)有了这样的认识和理解,我们理解spring AOP中的一些常用的概念就很简单:

    横切关注点:哪些业务需要拦截,拦截后干什么?

切面:若干个横切关注点的抽象结合。即:抽象出来的公共的业务逻辑单元

   连接点:需要拦截的业务。原本剩下的业务逻辑

切入点:连接点的表达式。和通知相似。

     通知:将共同的逻辑代码植入的提示。前置,后置,异常,返回,环绕。

1.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!--
<bean id="person" class="com.bean2.Person">
<constructor-arg value="12" type="int"></constructor-arg>
</bean>
--> <bean id="myMath" class="com.bean2.MyMath"></bean> <bean id="mylog" class="com.bean2.Log"></bean>
<!-- aop配置 -->
<aop:config>
<!--配置切入点
* com.bean.person.*(..):person类中的所有方法
* com.bean.*.*(..) bean包里的所有类的所有犯法
* com.bean.person.add*(int,int) :person 类中以add开头的方法,并且参数是两个int值
-->
<aop:pointcut expression="execution(* com.bean2.MyMath.*(..))" id="pc"/>
<!-- 切面 -->
<aop:aspect ref="mylog">
<!-- 通知
<aop:before method="beforelog" pointcut-ref="pc"/>
<aop:after method="afterlog" pointcut-ref="pc" />
<aop:after-returning method="returnLog" pointcut-ref="pc" returning="o"/>
<aop:after-throwing method="throwLog" pointcut-ref="pc" throwing="e"/>
--> <aop:around method="aroundLog" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>
</beans>

2.Log

 package com.bean2;

 import java.util.Arrays;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class Log { public void beforelog(JoinPoint jp){
String methodName = jp.getSignature().getName();
Object[] os = jp.getArgs();
System.out.println("正在运行"+methodName+"方法,参数是:"+Arrays.asList(os));
}
public void afterlog(JoinPoint jp){
String methodName = jp.getSignature().getName();
System.out.println(methodName+"方法运行完");
}
public void returnLog(JoinPoint jp,Object o){
String methodName = jp.getSignature().getName();
System.out.println(methodName+"方法的返回结果是:"+o);
}
public void throwLog(JoinPoint jp,Throwable e){
String methodName = jp.getSignature().getName();
System.out.println(methodName+"方法发生了异常,异常信息是:"+e.getMessage());
} public void aroundLog(ProceedingJoinPoint jp){
String methodName = jp.getSignature().getName();
Object[] os = jp.getArgs();
//前置
System.out.println("正在运行"+methodName+"方法,参数是:"+Arrays.asList(os));
//执行目标对象
try {
Object result = jp.proceed();
//返回
System.out.println(methodName+"方法的返回结果是:"+result);
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
//异常
System.out.println(methodName+"方法发生了异常,异常信息是:"+e.getMessage());
}finally{
//后置通知
System.out.println(methodName+"方法运行完");
}
}
}

3.MyMath

 package com.bean2;

 public class MyMath {

     public int add(int num1,int num2){

         int result = num1+num2;
return result;
}
public int sub(int num1,int num2){
//System.out.println("正在运行sub方法,参数是:"+num1+","+num2);
int result = num1-num2; //System.out.println("sub方法运行完,结果是:"+result);
return result;
}
public int div(int num1,int num2){
int result = num1/num2;
return result;
}
public int mul(int num1,int num2){
int result = num1*num2;
return result;
}
}

4.Person

 package com.bean2;

 public class Person {

     private double num1;
private int num2; public Person(double num){
System.out.println("double");
this.num1 = num;
}
public Person(int num){
System.out.println("int");
this.num2 = num;
}
public void show(){
System.out.println(num1+","+num2); } }

[原创]spring及springmvc精简版--AOP的更多相关文章

  1. [原创]spring及springmvc精简版--IOC

    本篇博客为自己学习spring和springmvc的一个总结.主要以代码为主,至于文字性描述理解性东西,可以自行百度.有认识不妥的地方,还望指出,相互学习. 以前很困惑spring中的一些概念,在学习 ...

  2. [原创]spring及springmvc精简版--继承数据源,声明式事物

    1.前期:导入c3p0 jar包,相关数据库连接jar包,我用的是mysql 2.关注事物管理器的配置和AOP配置 代码: 核心关注bean配置文件 application.xml <?xml ...

  3. Spring实战 (第3版)——AOP

    在软件开发中,分布于应用中多处的功能被称为横切关注点.通常,这些横切关注点从概念上是与应用的 业务逻辑相分离的(但是往往直接嵌入到应用的业务逻辑之中).将这些横切关注点与业务逻辑相分离正是 面向切面编 ...

  4. [原创] RT7 Lite win7旗舰版精简方案

    [原创] RT7 Lite win7旗舰版精简方案 墨雪SEED 发表于 2016-1-26 21:23:54  https://www.itsk.com/thread-362912-1-5.html ...

  5. Spring的第四天AOP之注解版

    Spring的第四天AOP之注解版 ssm框架 spring  在上一篇博客中,介绍了Spring的AOP的xml版本的使用,在这篇博客中,我将介绍一下,注解版的使用. 常用注解 注解 通知 @Aft ...

  6. Spring的第三天AOP之xml版

    Spring的第三天AOP之xml版 ssm框架 spring  AOP介绍 AOP(Aspect Oriented Programming),面向切面编程.它出来的目的并不是去取代oop,而是对它的 ...

  7. ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第十四天(非原创)

    文章大纲 一.淘淘商城总体架构介绍二.淘淘商城重要技术点总结三.项目常见面试题四.项目学习(all)资源下载五.参考文章 一.淘淘商城总体架构介绍 1. 功能架构   2. 技术选型 (1)Sprin ...

  8. Spring+Mybatis+SpringMVC+Maven+MySql搭建实例

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+SpringMVC+M ...

  9. Spring+Mybatis+SpringMVC后台与前台分页展示实例(附工程)(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文实现了一个后台由Spring+Mybatis+SpringMVC组成,分页采用Pag ...

随机推荐

  1. CF 482A(Diverse Permutation-相邻距离不同数为k的1~n全排列构造)

    A. Diverse Permutation time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. select option 不可以选

    <select> <option>Volvo</option> <option>Saab</option> <option disab ...

  3. Struts2 Action/动作

    动作是Struts2框架的核心,因为他们的任何MVC(模型 - 视图 - 控制器)框架.每个URL将被映射到一个特定的动作,它提供了来自用户的请求提供服务所需的处理逻辑. 但动作也提供其他两个重要的能 ...

  4. web.xml文件:

    在web.xml配置文件是一个的J2EE配置文件,决定如何处理HTTP请求servlet容器的元素.它不是严格意义上的Struts2的配置文件,但它是一个文件,需要配置Struts2的工作. 正如前面 ...

  5. 寒城攻略:Listo 教你用Swift 语言编写 IOS 平台流媒体播放器

    先展示播放器效果:   依然继承 Listo 本人的强迫症,还是从最初到完毕完整的写一个攻略来记录一下,这里声明 Listo 本人也是看了非常多的戴维营攻略才总结分享给大家这一篇攻略的. 首先,Lis ...

  6. iOS --发送手机验证码收不到手机验证码

    方法一:使用受信任设备上显示的验证码. 如果您的受信任设备是iOS 9 及以上系统时,则验证码将自动显示在受信任设备上.此时你信任的设备上会弹出你在某地登录ID位置的小地图,有一个选择允许与不允许登录 ...

  7. lumen model orm

    我尽量遍历写一遍Illuminate\Database\Query\Builder类的大部分方法 select设置查询字段 Notice::select('title')->get(); Not ...

  8. ofstream和ifstream详细用法

    ASCII和二进制文件的输入输出 First:包含头文件#include <fstream> ASCII输入: 首先要创建一个in-stream对象:ifstream fin(" ...

  9. 【BZOJ3730】震波 动态树分治+线段树

    [BZOJ3730]震波 Description 在一片土地上有N个城市,通过N-1条无向边互相连接,形成一棵树的结构,相邻两个城市的距离为1,其中第i个城市的价值为value[i].不幸的是,这片土 ...

  10. 【BZOJ4849】[Neerc2016]Mole Tunnels 模拟费用流

    [BZOJ4849][Neerc2016]Mole Tunnels Description 鼹鼠们在底下开凿了n个洞,由n-1条隧道连接,对于任意的i>1,第i个洞都会和第i/2(取下整)个洞间 ...