一、Srping AOP

  AOP(Aspect Oriented Programming)解释为面向切面编程,何为切面,用刀把一块面包切成两半,刀切下去形成的面就叫切面,那么面向切面的就是形成切面的这把,刀切在哪(切入点),怎么切(通知),切成什么样(通知实现方法),切的过程就是切面织入的过程。这种编程方式主要为了分离关注点并且能够增强类的辅助功能。比如做日志管理,权限控制等工作,在特定的时候和位置做特定的事情,而无需在原来方法中做特殊处理。

  AOP概念:

      • 切面:多个对象或方法要实现的交叉功能的集合。
      • 连接点:被关注的方法,执行了该方法会触发通知。
      • 切入点:一系列连接点的集合。
      • 通知:切面的实际实现,在连接点方法被执行的时候在适当的地方插入通知。
        1. before:前置通知,在方法被调用之前调用
        2. after:后置通知,在方法执行结束之后调用,无论方法是否执行成功
        3. around:环绕通知,在目标方法执行前和执行结束之后分别执行自定义方法
        4. after-return:在方法成功执行之后调用,
        5. after-throwing:在方法抛出异常后调用
      • 目标对象:被通知的对象,可以是自己的编写的类也可以是,第三方类对象。(连接点就是目标对象中的某些方法)
      • 代理:将通知应用到目标对象后创建的对象。
      • 织入:将切面应用到目标对象从而创建一个新的代理对象的过程。
      • 引入:为类添加新的方法和属性。(<aop:declare-parents ../>)

二、AOP配置范例:

  1.spring中的aop的xml配置方式简单实例

  2.spring中aop的注解实现方式简单实例

  3.利用切面为特定的类添加新功能:

<aop:config>
<!-- 两个切面-->
<aop:aspect ref="company">
<!-- 利用切面为特定的类添加新功能 -->
<aop:declare-parents types-matching="com.springAop.InstanceMine+" <!-- 实现了InstanceMine接口的实现类可以强转成InstanceOtheer类型的对象并使用他的实现类的方法的方法 -->
        implement-interface="com.springAop.InstanceOther"  
default-impl="com.springAop.OtherImpl"/>  <!--InstanceOther的默认实现类,当InstanceMine对象强转成InstanceOther的对象时,默认实现类为OtherImpl-->
</aop:aspect>
</aop:config>

  接口类:instanceMine和instanceOther

public interface InstanceMine {
public void speak1(String meg);
} public interface InstanceOther {
public void speak2(String meg);
}

  接口实现类: OtherImple和MineImpl

public class MineImpl implements instanceMine {

    public void speak1(String meg) {
System.out.println("s1");
}
} public class OtherImpl implements instanceOther {
public void speak2(String meg) {
System.out.println(meg);
}
}

  测试类:Test

public class Test {
@org.junit.jupiter.api.Test
public void TestDemo(){
ApplicationContext app = new FileSystemXmlApplicationContext("src/main/java/com/springAop/bean.xml"); InstanceMine mine = app.getBean("mine",instanceMine.class);
((InstanceOther)mine).speak2("我可以说话吗");//强转成InstanceOther,并调用实现类OtherImpl的实现方法
}
}

  结果:

  

三.以代理对象的方式实现AOP:

  首先理解一下三种代理模式:理解三种代理模式

  1.前置通知:

 /**
* 前置通知:实现MethodBeforeAdvice接口,在目标方法执行之前执行
* 相当于aop配置<aop:before method="before" pointcut-ref="当前类的bean"/>
*/
public class BeforeNotify implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("=========== 前置通知 =========");
}
}

  

  2.后置通知:

 /**
* 后置(返回)通知:在方法结束返回时调用,一般包含在环绕通知结束前执行,
* 方法成功执行有效,异常结束则该方法无效。
* 相当于aop配置:<aop:after-returning method="afterReturning" pointcut-ref="当前类的bean"/>
*/
public class AfterNotify implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("=========== 后置通知 =========");
}
}

  

  3.环绕通知:

 /**
* 环绕通知:在方法执行前和执行后均可以执行自定义的方法,
* 相当于aop配置:<aop:around method="invoke" pointcut-ref="当前类的Bean"/>
*/
public class AroundNotify implements MethodInterceptor { public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("=========== 环绕通知 =========");
Object obj = methodInvocation.proceed();//执行目标方法
System.out.println("=========== 环绕通知 =========");
return obj;
}
}

  

  4.异常通知:

 /**
* 异常通知:在目标方法执行异常时执行,
* 注意:ThrowsAdvice是个空接口,里面未定义任何待实现类
* 相当于aop配置:<aop:after-throwing method="afterThrowing" pointcut-ref="当前类的bean"/>
*/
public class ExceptionNotify implements ThrowsAdvice {
public void afterThrowing(Method method, Object[] args, Object target, Exception ex){
System.out.println("方法:"+method.getName()+"出错了\n原因:"+ex.getMessage());
}
}

  此处的ThrowAdvice接口是没有实现方法的,但是又不允许随便定义,在源码中我们看到了规定的几个方法,因为此接口中,没有任何实现方法,当被利用反射机制调用的时候,必须实现一下方法中的一种,这是源码注释的说明:

  5.bean配置

 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 被代理对象 -->
<bean class="com.springAop.proxyAOP.UserDao" id="userDao"/> <!-- 关联通知 -->
<bean class="com.springAop.proxyAOP.BeforeNotify" id="before"/>
<bean class="com.springAop.proxyAOP.AroundNotify" id="aroundNotify"/>
<bean class="com.springAop.proxyAOP.ExceptionNotify" id="exceptionNotify"/>
<bean class="com.springAop.proxyAOP.AfterNotify" id="afterNotify"/> <!-- 代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 代理对象接口,接口方法就是连接点 -->
<property name="proxyInterfaces">
<list>
<!-- 目标对象实现的接口类,实现了那些接口都可以补充进去,接口的方法就是一个个连接点 -->
<value>com.springAop.proxyAOP.IUserDao</value>
<value>com.springAop.proxyAOP.BookDao</value>
</list>
</property> <!-- 关联通知类型 -->
<property name="interceptorNames">
<list>
<value>before</value><!-- 前置通知织入 -->
<value>aroundNotify</value><!-- 环绕通知织入 -->
<value>exceptionNotify</value><!-- 异常通知织入 -->
<value>afterNotify</value><!-- 后置通知织入 -->
</list>
</property> <!-- 关联被代理对象(aop的目标对象) -->
<property name="target" ref="userDao"/>
</bean>
</beans>

  6.目标对象:(实现了两个接口)

 public class UserDao implements IUserDao,BookDao{
public void save() {
System.out.println("保存用户信息中。。。。");
// int i=1/0;
} public void getBook() {
System.out.println("拿到一本书");
}
}

  7.测试类:

 public class Test{
@org.junit.jupiter.api.Test
public void testDemo(){
ApplicationContext app = new FileSystemXmlApplicationContext("file:G:\\DevelopSoftware\\IDEA\\workspace\\springDemo\\src\\main\\java\\com\\springAop\\proxyAOP\\bean.xml");
/**
* 获取的bean是目标对象的代理对象,可以将代理对象强转成任何目标对象实现的接口对象
*/
IUserDao userDao = app.getBean("proxyFactoryBean",IUserDao.class);
userDao.save();
System.out.println("=========================");
/**
* 将代理对象强转成BookDao接口对象,并调用该接口方法。
*/
((BookDao)userDao).getBook();
}
}

  运行结果:

  

Spring AOP梳理的更多相关文章

  1. Spring Aop 梳理

    Aspect Oriented Programming  面向切面编程.解耦是程序员编码开发过程中一直追求的.AOP也是为了解耦所诞生. 具体思想是:定义一个切面,在切面的纵向定义处理方法,处理完成之 ...

  2. [转]彻底征服 Spring AOP 之 理论篇

    基本知识 其实, 接触了这么久的 AOP, 我感觉, AOP 给人难以理解的一个关键点是它的概念比较多, 而且坑爹的是, 这些概念经过了中文翻译后, 变得面目全非, 相同的一个术语, 在不同的翻译下, ...

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

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

  4. 转载:Spring AOP (下)

    昨天记录了Spring AOP学习的一部分(http://www.cnblogs.com/yanbincn/archive/2012/08/13/2635413.html),本来是想一口气梳理完的.但 ...

  5. (转)spring aop(下)

    昨天记录了Spring AOP学习的一部分(http://www.cnblogs.com/yanbincn/archive/2012/08/13/2635413.html),本来是想一口气梳理完的.但 ...

  6. 彻底征服 Spring AOP 之 理论篇

    基本知识 其实, 接触了这么久的 AOP, 我感觉, AOP 给人难以理解的一个关键点是它的概念比较多, 而且坑爹的是, 这些概念经过了中文翻译后, 变得面目全非, 相同的一个术语, 在不同的翻译下, ...

  7. Spring AOP 学习例子

    http://outofmemory.cn/code-snippet/3762/Spring-AOP-learn-example     工作忙,时间紧,不过事情再多,学习是必须的.记得以前的部门老大 ...

  8. Spring AOP 知识点入门

    一.基本知识点 1.AOP概念 AOP(Aspect-Oriented Programming), 即 面向切面编程, 它与 OOP( Object-Oriented Programming, 面向对 ...

  9. Spring AOP概述

    一.AOP的基本概念: 首先先给出一段比较专业的术语: 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的 ...

随机推荐

  1. css 把图片变为为黑白

    img{ -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grayscale(100%); -o- ...

  2. Apollo阿波罗配置中心docker

    前言 在分布式系统中,要改个配置涉及到很多个系统,一个一个改效率低下,吃力不讨好.用配置中心可以解决这个问题.当然配置中心有不少,以下对比的表格是照搬Apollo Wiki的. 功能点 Apollo ...

  3. MySQL数据库基础(MySQL5.7安装、配置)

      写在前面: MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQ ...

  4. WordPress 实现附件上传自动重命名但不改变附件标题

    WordPress 上传媒体文件时,默认会保持文件名不变.如果上传文件名中包含中文字符,则会造成部分浏览器显示的文件 URL 疑似乱码甚至无法访问.网上流行较广的是通过注册  wp_handle_up ...

  5. org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [/Users/lonecloud/tomcat/apache-tomcat-7.0.70 2/webapps/myproject/WEB-INF/classes/cn/lone

    解决这个报错的解决办法: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidat ...

  6. js中boolean类型的理解

    <html> <head> <script type="text/javascript"> var x="12"; aler ...

  7. 分布式高性能消息处理中心HPMessageCenter

    # HPMessageCenter 高性能消息分发中心.用户只需写好restful接口,在portal里面配置消息的处理地址,消息消费者就会自动访问相关接口,完成消息任务. ### 部署说明 **创建 ...

  8. Redis的两种持久化方式-快照持久化和AOF持久化

    Redis为了内部数据的安全考虑,会把本身的数据以文件形式保存到硬盘中一份,在服务器重启之后会自动把硬盘的数据恢复到内存(redis)的里边,数据保存到硬盘的过程就称为"持久化"效 ...

  9. UVA - 658 最短路

    思路:通过前后两种状态建立一条边,利用Dijsktra就可以做了. 注意利用二进制优化. AC代码 #include <cstdio> #include <cmath> #in ...

  10. SpringBoot SpringSecurity4整合,灵活权限配置,弃用注解方式.

    SpringSecurity 可以使用注解对方法进行细颗粒权限控制,但是很不灵活,必须在编码期间,就已经写死权限 其实关于SpringSecurity,大部分类都不需要重写,需要的只是妥善的配置. 每 ...