SpringAOP面向切面编程
Spring中三大核心思想之一AOP(面向切面编程):
<?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"
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.2.xsd">
<!-- 定义bean -->
<bean id="stuService" class="com.lxit.aop.service.StudentService" />
<!-- AOP配置 -->
<!-- 添加Aspect的bean -->
<bean id="logAspect" class="com.lxit.aop.aspect.LogAspect" />
<aop:config>
<!-- 定义一个pointcut -->
<aop:pointcut id="servicepointcut"
expression="execution(* com.lxit.aop.service.*.*(..))" />
<!-- 定义aspect,引用生成的aspectBean 并指定pointcut 和 method-->
<aop:aspect id="aspect1" ref="logAspect">
<aop:after pointcut-ref="servicepointcut" method="logAdd" />
</aop:aspect>
</aop:config>
</beans>
public static void main(String[] args) {
ApplicationContext ac =
new ClassPathXmlApplicationContext("spring.xml");
StudentService service = (StudentService) ac.getBean("stuService");
service.add();
service.getStudent();
}
异常抛出增强:
异常抛出增强的特点是在目标方法抛出异常时织入增强处理,
但是异常处理一般会需要获取异常参数。
在配置文件中添加异常处理的aspect。
使用<aop:after-throwing来进行异常织入。
public class ExceptionAspect {
public void exceptionLog(Exception e){
System.out.println("发生异常,写入日志。" + e.getMessage());
}
}
<bean id="exceptionAspect" class="com.lxit.aop.aspect.ExceptionAspect" />
<aop:config>
<!-- 定义一个pointcut -->
<aop:pointcut id="servicepointcut"
expression="execution(* com.lxit.aop.service.*.*(..))" />
<!-- 定义aspect,引用生成的aspectBean 并指定pointcut 和 method-->
<aop:aspect id="aspect2" ref="exceptionAspect">
<!-- 表示当程序发生异常后才织入 -->
<aop:after-throwing method="exceptionLog"
pointcut-ref="servicepointcut" throwing="e"/>
</aop:aspect>
</aop:config>
环绕增强:
环绕增强在目标方法的前后都可以织入增强处理
环绕增强是功能最强大的增强处理,Spring把目标方法的控制权全部交给了它
在环绕增强处理中,可以获取或修改目标方法的参数、返回值,可以对它进行异常处理,甚至可以决定目标方法是否执行
public class AroundLogger {
public Object aroundLogger(ProceedingJoinPoint jp) throws Throwable { … }
}
<bean id="theLogger" class="aop. AroundLogger"></bean>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* biz.IUserBiz.*(..))" />
<aop:aspect ref="theLogger">
<aop:around method="aroundLogger" pointcut-ref="pointcut" />
</aop:aspect>
</aop:config>
五中织入方式的区别:
<aop:before …>:在目标方法调用之前织入。
只要before方法执行完成,目标方法总会被调用,但before可以通过抛出异常来阻止目标方法执行,before不能访问目标方法的返回值。
<aop:after…>:在目标方法调用之后织入。
after不能组织目标方法的执行,after不能访问目标方法的返回值。
<aop:after-throwing..>:抛出异常时织入。如果指定throwing必须指定一个异常参数,增强方法中必须和此参数同名,类型必须大于该异常类型。
<aop:after-returning…>:在目标方法成功执行之后织入。
after-returning:不能阻止目标方法的执行,可以访问目标方法的返回值,但不能修改。
<aop:around…>:在目标方法调用之前和调用之后织入。它的处理方法必须包含一个ProceedingJoinPoint形参。
aop:around:可以组织目标方法的执行,可以访问目标方法的返回值,可以修改返回值。
以上增强器都可以指定args来指定参数
在Struts2,hibernate,Spring整合中就可以看到一种很好的效果。
在serivce层定义成一个切点在执行操作前可以开启一系列操作,比如写入日志,事务等操作是一种典型的案例。
AOP使用场景
AOP用来封装横切关注点,具体可以在下面的场景中使用:
Authentication 权限 Caching 缓存 Context passing 内容传递 Error handling 错误处理 Lazy loading 懒加载
Debugging 调试 logging, tracing, profiling and monitoring 记录跟踪 优化 校准 Performance optimization 性能优化
Persistence 持久化 Resource pooling 资源池 Synchronization 同步 Transactions 事务
SpringAOP面向切面编程的更多相关文章
- JavaWeb_(Spring框架)SpringAOP面向切面编程
SpringAOP:面向切面编程(面向fifter编程) 通俗易懂术语:所有纵向重复的代码,我们提取成横向的代码 以下文章内容参考知乎:从0带你学习SpringAOP,彻底的理解AOP思想 传送门 1 ...
- SpringAOP 面向切面编程
AOP的相关概念 AOP:全称是 Aspect Oriented Programming 即:面向切面编程. 简单的说它就是把我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改 ...
- Spring-AOP面向切面编程
AOP是面向切面编程,区别于oop,面向对象,一个是横向的,一个是纵向. 主要解决代码分散和混乱的问题. 1.概念: 切面:实现AOP共有的类 通知:切面类中实现切面功能的方法 连接点:程序被通知的特 ...
- 了解并使用springAOP(面向切面编程)
Aop是干嘛的为什么要使用它 在业务系统中,总有一些散落,渗透到系统的各处且不得不处理的事情,这些穿插在既定业务中的操作就是所谓的“横切逻辑”,也称切面, 我们怎样才不受这些附加要求的干扰,专心于真正 ...
- Spring--AOP(面向切面)编程
AOP 切面就像一把菜刀,将Java处理业务流程进行分割,在分割处添加特定的业务处理.主要应用于声明事务.安全和缓存.在本文中,主要介绍两种切面的实现方法--Java配置和XML配置. Java配置 ...
- AOP面向切面编程的四种实现
一.AOP(面向切面编程)的四种实现分别为最原始的经典AOP.代理工厂bean(ProxyFacteryBean)和默认自动代理DefaultAdvisorAutoProxyCreator以及Bea ...
- spring入门(四)【面向切面编程】
开发过程中很多时候会用到日志.事务等操作,这些操作如果要写在业务代码中会相当麻烦,这时就会用到面向切面编程(AOP),AOP作为一种编程思想,和OOP有着不同的侧重点,面向对象侧重于万事万物皆对象,而 ...
- 面向切面编程AOP
本文的主要内容(AOP): 1.AOP面向切面编程的相关概念(思想.原理.相关术语) 2.AOP编程底层实现机制(动态代理机制:JDK代理.Cglib代理) 3.Spring的传统AOP编程的案例(计 ...
- Spring面向切面编程(AOP)
1 spring容器中bean特性 Spring容器的javabean对象默认是单例的. 通过在xml文件中,配置可以使用某些对象为多列. Spring容器中的javabean对象默认是立即加载(立即 ...
随机推荐
- 第20月第28天 tensorflow
1. 505 sudo pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade virtualenv 506 virt ...
- Hbase思维导图之架构
- RSA加解密
RSA加密解密及数字签名Java实现 RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一 ...
- Spring Cloud学习资料
博客 1.跟我学Spring Cloud 2.周立|Spring Cloud 3.Spring Cloud基础教程(强烈推荐) 4.Spring Cloud系列文章 5.forezp|史上最简单的 S ...
- Java-Servlet -Helloworld
Servlet 简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向浏 ...
- XLMHttpRequest对象的status属性,readyState属性以及onreadystatechange事件
注:XLMHttpRequest简写为XHR 一.HTTP请求过程 (1)建立TCP链接 (2)web浏览器向web服务器发送请求命令 (3)web浏览器发送请求头信息 (4)web服务器应答 (5) ...
- Django中间件基础笔记
django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在django项目的se ...
- FLASK-----基本知识(一)
中文文档(http://docs.jinkan.org/docs/flask/) 英文文档(http://flask.pocoo.org/docs/0.11/) FLASK介绍 Flask是一个基于P ...
- C/C++ assert()函数用法总结
1. 简介 assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行. 原型定义: #include <assert.h>void ass ...
- Des加密解密算法java实现
package tech.fullink.eaglehorn.utils; import javax.crypto.Cipher; import javax.crypto.SecretKey; imp ...