cglib动态代理(即AOP)
Computer.java
package com.wh.spring_aop;
public class Computer {
public void playLOL(){
System.out.println("LOL进行中...");
}
public String result(boolean flag){
System.out.println("公布比赛结果...");
if(flag){
return "您赢了";
}else{
int a=10/0;
return "您输了";
}
}
}
AopProxy.java
package com.wh.spring_aop; import org.aspectj.lang.JoinPoint; /**
* 完成AOP的步骤
1、切入点程序。
2、切面程序。
3、通过配置文件将切面程序插入到切入点程序的某个位置上(通知)
*
*/
public class AopProxy { public void addBefore(JoinPoint jp){
System.out.println("AOP前置通知!");
} public void addAfterReturn(Object returnResult){
if(returnResult!=null){
System.out.println("AOP后置通知!"+returnResult);
}
System.out.println("AOP后置通知");
} public void addAfter(){
System.out.println("AOP最终通知");
} public void addThrow(Throwable e){
System.out.println("AOP异常通知 异常信息是: "+e.getMessage());
} }
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <bean id="computer" class="com.wh.spring_aop.Computer" />
<bean id="aopProxy" class="com.wh.spring_aop.AopProxy" /> <aop:config>
<!-- 虽然返回值类型可以任意 方法名任意 参数类型任意 ,但是参数个数不是任意的,有几个星号就代表有几个参数,参数个数不匹配,则不会执行aop -->
<aop:pointcut expression="execution(* com.wh.spring_aop.Computer.*(*))" id="computerCut" /><!--此处为切入点 -->
<aop:aspect ref="aopProxy"><!-- 此处为切面 -->
<!-- AOP前置通知 -->
<aop:before method="addBefore" pointcut-ref="computerCut" /><!-- 此处为通知 -->
<!-- AOP后置通知:在目标组件的方法正常执行并返回参数后执行的程序。
切面程序:后置通知的切面程序中可以获取到目标方法返回参数,但需要在配置文件中声明参数名,依赖spring容器注入参数值。
returning="returnResult"里的值要与AopProxy里的方法里的参数名一致,否则报错
-->
<aop:after-returning method="addAfterReturn" pointcut-ref="computerCut" returning="returnResult"/>
<!-- AOP异常通知:在目标组件的方法抛出异常信息后执行的程序。
切面程序:异常通知的切面程序中可以获取到目标组件抛出的异常信息,需要在配置文件上声明异常参数名,依赖spring容器注入参数值。
-->
<aop:after-throwing method="addThrow" pointcut-ref="computerCut" throwing="e"/>
<!-- AOP最终通知:在目标组件的方法正常执行后执行,或在异常通知之前执行。 -->
<aop:after method="addAfter" pointcut-ref="computerCut"/> </aop:aspect>
</aop:config> </beans>
TestMVC.java
package com.wh.spring_aop; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestMVC { @Test
public void test01(){
@SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Computer computer =(Computer)ac.getBean("computer");
//computer.playLOL();
computer.result(false);
} }
运行结果:
AOP前置通知!
公布比赛结果...
AOP异常通知 异常信息是: / by zero
AOP最终通知
cglib动态代理(即AOP)的更多相关文章
- 动态代理的两种方式,以及区别(静态代理、JDK与CGLIB动态代理、AOP+IoC)
Spring学习总结(二)——静态代理.JDK与CGLIB动态代理.AOP+IoC 目录 一.为什么需要代理模式 二.静态代理 三.动态代理,使用JDK内置的Proxy实现 四.动态代理,使用cg ...
- Java之代理(jdk静态代理,jdk动态代理,cglib动态代理,aop,aspectj)
一.概念 代理是什么呢?举个例子,一个公司是卖摄像头的,但公司不直接跟用户打交道,而是通过代理商跟用户打交道.如果:公司接口中有一个卖产品的方法,那么公司需要实现这个方法,而代理商也必须实现这个方法. ...
- Spring学习总结(二)——静态代理、JDK与CGLIB动态代理、AOP+IoC
一.为什么需要代理模式 假设需实现一个计算的类Math.完成加.减.乘.除功能,如下所示: package com.zhangguo.Spring041.aop01; public class Mat ...
- Java 动态代理及AOP实现机制
AOP实现机制http://www.iteye.com/topic/1116696 AOP: (Aspect Oriented Programming) 面向切面编程AOP包括切面(aspect).通 ...
- 【Java EE 学习 51】【Spring学习第三天】【cglib动态代理】【AOP和动态代理】【切入点表达式】
一.cglib动态代理 1.简介 (1)CGlib是一个强大的,高性能,高质量的Code生成类库.它可以在运行期扩展Java类与实现Java接口. (2) 用CGlib生成代理类是目标类的子类. (3 ...
- Spring AOP详解 、 JDK动态代理、CGLib动态代理
AOP是Aspect Oriented Programing的简称,面向切面编程.AOP适合于那些具有横切逻辑的应用:如性能监测,访问控制,事务管理以及日志记录.AOP将这些分散在各个业务逻辑中的代码 ...
- CgLib动态代理学习【Spring AOP基础之一】
如果不了解JDK中proxy动态代理机制的可以先查看上篇文章的内容:Java动态代理学习[Spring AOP基础之一] 由于Java动态代理Proxy.newProxyInstance()的时候会发 ...
- 【转载】Spring AOP详解 、 JDK动态代理、CGLib动态代理
Spring AOP详解 . JDK动态代理.CGLib动态代理 原文地址:https://www.cnblogs.com/kukudelaomao/p/5897893.html AOP是Aspec ...
- Spring AOP中的JDK和CGLib动态代理哪个效率更高?
一.背景 今天有小伙伴面试的时候被问到:Spring AOP中JDK 和 CGLib动态代理哪个效率更高? 二.基本概念 首先,我们知道Spring AOP的底层实现有两种方式:一种是JDK动态代理, ...
- spring---aop(4)---Spring AOP的CGLIB动态代理
写在前面 前面介绍了Spring AOP的JDK动态代理的过程,这一篇文章就要介绍下Spring AOP的Cglib代理过程. CGLib全称为Code Generation Library,是一个强 ...
随机推荐
- Multisim破解教程
转载:http://www.121down.com/article/article_52879.html
- 在Eclipse中设置Maven插件
[步骤] Maven插件的设置: ①installations:指定Maven核心程序的位置.不建议使用Maven插件自带的Maven程序,而应该使用我们自己解压的那个. ②user settings ...
- SQL 快速参考-----http://www.runoob.com/sql/sql-quickref.html
http://www.runoob.com/sql/sql-quickref.html SQL 快速参考
- 武大OJ 706.Farm
Farmer John has a farm. Betsy, a famous cow, loves running in farmer John's land. The noise she made ...
- HTML表单数据转JSON
问题描述 后端使用如下方式接收前端传入参数: @PostMapping(value = "/test", produces = MediaType.APPLICATION_JSON ...
- jQyery 整体架构
jQuery的模块 一.jQuery一共有13个模块: 1. 核心方法 2. 回调模块(callbacks) 3. 数据缓存 4. 异步队列(Deffered) 5. 选择器操做 6. 属性操作 7. ...
- The Secant Method(正割法、弦截法) 附C语言代码
弦截法是一种求方程根的基该方法,在计算机编程中经常使用. 他的思路是这种:任取两个数x1.x2,求得相应的函数值f(x1).f(x2).假设两函数值同号,则又一次取数.直到这两个函数值异号为止. 连接 ...
- 玩转CPU之直线
近期在看编程之美,看到第一个问题时,一下子就被吸引了,原来在windows 的任务管理器中还能够让CPU舞动起来,再一次的相信了编程中仅仅有想不到没有做不到,对于书中的做法和网上的实现大致都同样.只是 ...
- CentOS6.5下用Git克隆代码(https方式)
一.首先最好保证GIT是最新版 查看GIT命令 $ git --version 有关git的安装,应该有好多文章介绍.注意更新之后,要重启系统,否则显示的版本号,还是老版本. 二.如果工作环境存在网络 ...
- Linux ALSA声卡驱动之六:ASoC架构中的Machine
前面一节的内容我们提到,ASoC被分为Machine.Platform和Codec三大部分,其中的Machine驱动负责Platform和Codec之间的耦合以及部分和设备或板子特定的代码,再次引用上 ...