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,是一个强 ...
随机推荐
- Leetcode 51.N后问题
N后问题 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后问题的解决方案. ...
- c++ 上机实验题
c++语言俺是不会啦,但是朋友考试需要,那只能勉为其难的入门下做做考试题了. 以下就是具体的题目和答案: ----------------------------------------------- ...
- 这个贴子的内容值得好好学习--实例详解Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化
感觉要DJANGO用得好,ORM必须要学好,不管理是内置的,还是第三方的ORM. 最最后还是要到SQL.....:( 这一关,慢慢练啦.. 实例详解Django的 select_related 和 p ...
- Yet another Number Sequence 矩阵快速幂
Let’s define another number sequence, given by the following function: f(0) = a f(1) = b f(n) = f(n ...
- 基本的数据类型分析----java.lang.Number类及其子类分析
本文转自http://blog.csdn.net/springcsc1982/article/details/8788345 感谢作者 编写了一个测试程序,如下: int a = 1000, b= 1 ...
- PHP array_pad()
定义和用法 array_pad() 函数向一个数组插入带有指定值的指定数量的元素. 语法 array_pad(array,size,value) 参数 描述 array 必需.规定数组. size 必 ...
- 1. MaxCounters 计数器 Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum.
package com.code; import java.util.Arrays; public class Test04_4 { public static int[] solution(int ...
- Behavioral模式之Interpreter模式
1.意图 给定一个语言,定义它的文法的一种表示.并定义一个解释器,这个解释器使用该表示来解释语言中的句子. 2.别名 无 3.动机 假设一种特定类型的问题发生的频率足够高,那么可能就值得将该问题的各种 ...
- 关于Android中的四大组件(Service的开启与关闭)
前言 服务(Service)是Android系统中的四大组件之中的一个.服务主要用于两个目的:后台执行和跨进程訪问. 通过启动 一个服务.能够在不显示界面的前提下在后台执行指定的任务,这样能够不影响用 ...
- CoffeeScript的缩进
CoffeeScript用缩进代替了花括符"{}",作用范围的划分只靠缩进.这带来代码精简.简洁的同时,有时候也让人困惑. 比如说: if 3 > 1 alert(" ...