一、Spring AOP简介(百度百科)

面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是 Spring

框架中的一个重要内容。利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度

降低,提高程序的可重用性,同时提高了开发的效率。

主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理等等。

二、Spring AOP实例

(1,前置通知;2,后置通知;3,环绕通知;4,返回通知;5,异常通知;)

<?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.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 业务逻辑bean -->
<bean id="userService" class="top.ruandb.service.impl.UserServiceImpl" ></bean>
<!-- 自定义的切面 -->
<bean id="userServiceAspect" class="top.ruandb.advice.UserServiceAspect"></bean>
<!-- AOP配置 -->
<aop:config>
<!-- 定义切面 -->
<aop:aspect id="userServiceAspect" ref="userServiceAspect">
<!-- 定义切点,表达式: -->
<aop:pointcut expression="execution(* top.ruandb.service.*.*(..))" id="businessService"/>
<!-- 前置通知,在方法执行之前通知 -->
<aop:before method="doBefore" pointcut-ref="businessService"/>
<!-- 后置通知:在方法执行之后通知 -->
<aop:after method="doAfter" pointcut-ref="businessService"/>
<!-- 环绕通知:在方法前后环绕 -->
<aop:around method="doAround" pointcut-ref="businessService"/>
<!-- 返回通知:方法返回后通知 -->
<aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>
<!-- 异常通知:出现异常后通知 -->
<aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>
</aop:aspect>
</aop:config> </beans>
//切面
public class UserServiceAspect { public void doBefore(JoinPoint jp) {
System.out.println(jp.getTarget().getClass().getName()+"前置通知:添加用户之前");
}
public void doAfter(JoinPoint jp) {
System.out.println(jp.getTarget().getClass().getName()+"后置通知:添加用户之后");
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println(pjp.getTarget().getClass().getName()+"环绕通知:环绕前");
Object obj = pjp.proceed();
System.out.println(pjp.getTarget().getClass().getName()+"环绕通知:环绕后");
return obj;
}
public void doAfterReturning(JoinPoint jp) {
System.out.println(jp.getTarget().getClass().getName()+"返回通知:返回通知");
}
public void doAfterThrowing(JoinPoint jp,Throwable ex) {
System.out.println(jp.getTarget().getClass().getName()+"异常通知:程序异常了"+ex.getMessage());
}
} public class SpringTest {
ApplicationContext ac ;
@Before
public void setUp() {
ac = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@After
public void tearDown() {
ac = null;
} @Test
public void test1() {
UserServiceI userService = (UserServiceI) ac.getBean("userService") ;
userService.addUser("rdb"); }
} 结果:
top.ruandb.service.impl.UserServiceImpl前置通知:添加用户之前
top.ruandb.service.impl.UserServiceImpl环绕通知:环绕前
添加用户rdb
top.ruandb.service.impl.UserServiceImpl返回通知:返回通知
top.ruandb.service.impl.UserServiceImpl环绕通知:环绕后
top.ruandb.service.impl.UserServiceImpl后置通知:添加用户之后 结果(异常):
top.ruandb.service.impl.UserServiceImpl前置通知:添加用户之前
top.ruandb.service.impl.UserServiceImpl环绕通知:环绕前
添加用户rdb
top.ruandb.service.impl.UserServiceImpl异常通知:程序异常了/ by zero
top.ruandb.service.impl.UserServiceImpl后置通知:添加用户之后

Spring总结之AOP的更多相关文章

  1. Spring 3.0 AOP (一)AOP 术语

    关于AOP.之前我已写过一个系列的随笔: <自己实现简单的AOP>,它的关注点在于实现.实现语言是C#,实现方式为 自定义实现 RealProxy 抽象类.重写Invoke方法,以便进行方 ...

  2. Spring系列之AOP实现的两种方式

    AOP常用的实现方式有两种,一种是采用声明的方式来实现(基于XML),一种是采用注解的方式来实现(基于AspectJ). 首先复习下AOP中一些比较重要的概念: Joinpoint(连接点):程序执行 ...

  3. springMVC+MyBatis+Spring 整合(4) ---解决Spring MVC 对AOP不起作用的问题

    解决Spring MVC 对AOP不起作用的问题 分类: SpringMVC3x+Spring3x+MyBatis3x myibaits spring J2EE2013-11-21 11:22 640 ...

  4. 【转】spring - ioc和aop

    [转]spring - ioc和aop 1.程序中为什么会用到spring的ioc和aop 2.什么是IOC,AOP,以及使用它们的好处,即详细回答了第一个问题 3.原理 关于1: a:我们平常使用对 ...

  5. Spring核心框架 - AOP的原理及源码解析

    一.AOP的体系结构 如下图所示:(引自AOP联盟) 层次3语言和开发环境:基础是指待增加对象或者目标对象:切面通常包括对于基础的增加应用:配置是指AOP体系中提供的配置环境或者编织配置,通过该配置A ...

  6. Spring中的AOP

    什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...

  7. Spring IOC及AOP学习总结

    一.Spring IOC体系学习总结: Spring中有两个容器体系,一类是BeanFactory.还有一类是ApplicationContext.BeanFactory提供了基础的容器功能.Appl ...

  8. Spring自学教程-AOP学习(五)

    Spring中的AOP 一.概述 (一)基本概念 1.什么是AOP?     面向方面编程.所谓方面即是指日志.权限.异常处理.事务处理等. 2.AOP的3个关键概念    (1)切入点(Pointc ...

  9. Spring 3.0 Aop 入门

    关于Aop的原理,动态代理,反射,之类的底层java技术网上搜一堆一堆的..我就不多说了,主要说在spring上使用aop的方法. 首先不得不说一下的就是,spring aop的支持需要外部依赖包: ...

  10. J2EE进阶(十四)超详细的Java后台开发面试题之Spring IOC与AOP

    J2EE进阶(十四)超详细的Java后台开发面试题之Spring IOC与AOP 前言   搜狐畅游笔试题中有一道问答题涉及到回答谈谈对Spring IOC与AOP的理解.特将相关内容进行整理.    ...

随机推荐

  1. Mac设置charles证书信任

  2. postman环境变量配置的详细过程(步骤加截图)

    环境变量的配置 实战:https://www.baidu.com/s?wd=博客园 使用cmd命令模式输入代码:Nslookup www.baidu.com 模拟环境:线上环境14.215.177.3 ...

  3. Docker与k8s的恩怨情仇(一)—成为PaaS前浪的Cloud Foundry

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 大家在工作中或许或多或少都接触过Docker,那你知道Docker以及容器化背后的原理到底是什么吗? 容器化 ...

  4. 「Spring Boot架构」集成Mybatis-Plus的实例详解

    MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 特性 无侵入:只做增强不做改变,引入它不会对现有工程 ...

  5. 【复习】Listening and Reading Comprehension

    短对话 M: Why do you declare the news that you're pregnant on your blog directly? W: I'm so excited tha ...

  6. Netty 框架学习 —— 引导

    概述 前面我们学习了 ChannelPipeline.ChannelHandler 和 EventLoop 之后,接下来的问题是:如何将它们组织起来,成为一个可实际运行的应用程序呢?答案是使用引导(B ...

  7. Linux 中的 AutoHotKey 键映射替代方案

    1. Windows 之 AutoHotKey 初次了解AutoHotKey,是在Win 下最爱效率神器:AutoHotKey | 晚晴幽草轩这篇博客中,博主有对AutoHotKey作详细介绍,这里不 ...

  8. C#调用JAVA(一)制作jar包

    1 //AndroidJavaClass类可以理解为某一个类 2 //AndroidJavaObject类可以理解为某一个类中的一个对象 打开AS,创建一个空项目 输入项目名,包名,本地路径以及语言之 ...

  9. Cloudflare DDoS配置案例

    导航: 这里将一个案例事项按照流程进行了整合,这样查看起来比较清晰.部分资料来自于Cloudflare 1.DDoS介绍 2.常用DDoS攻击 3.DDoS防护方式以及产品 4.Cloudflare ...

  10. 从零开始学前端,React框架背后的核心机制和原理JSX

    什么是React React是起源于Facebook的一个前端框架,用于构建用户界面的JavaScript库,Facebook用来探索一种更加高效优雅的Javascript MVC框架来架设Insta ...