spring的cglib代理

1、被代理类Person.java
package com.xiaostudy; /**
* @desc 被代理类
*
* @author xiaostudy
*
*/
public class Person { public void add() {
System.out.println("add()>>>>>>>>");
} public void update() {
System.out.println("update()>>>>>>>>");
} public void delete() {
System.out.println("delete()>>>>>>>>");
} }
2、切面类MyAdvice.java
package com.xiaostudy; /**
* @desc 切面类
*
* @author xiaostudy
*
*/
public class MyAdvice { /**
* @desc 植入代理方法的方法
*/
public void before() {
System.out.println("日记开始>>>>>>>>>>>");
} public void after() {
System.out.println("日记结束<<<<<<<<<<<<");
} }
3、代理工厂类MyBeanFactory.java
package com.xiaostudy; import java.lang.reflect.Method; import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy; /**
* @desc 代理工厂类
*
* @author xiaostudy
*
*/
public class MyBeanFactory {
/**
* @desc 获取一个代理的对象
*
* @return Person对象
*/
public static Person createPerson() {
// 被代理类
final Person person = new Person();
// 切面类
final MyAdvice myAdvice = new MyAdvice();
// 代理类,采用cglib
// 核心类
Enhancer enhancer = new Enhancer();
// 确定父类
enhancer.setSuperclass(person.getClass());
/*
* 设置回调函数 , MethodInterceptor接口 等效 InvocationHandler
* 接口 intercept() 等效 invoke()
* 参数1、参数2、参数3:以invoke一样
* 参数4:methodProxy 方法的代理
*/
enhancer.setCallback(new MethodInterceptor() { @Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
throws Throwable {
myAdvice.before();
Object obj = method.invoke(person, args);
// 这个与上面 等价
// Object obj2 = methodProxy.invokeSuper(proxy, args);
myAdvice.after();
return obj;
}
});
// 创建代理
Person obj = (Person) enhancer.create(); return obj;
} }
4、测试类Test.java
package com.xiaostudy; /**
* @desc 测试类
*
* @author xiaostudy
*
*/
public class Test { public static void main(String[] args) {
Person person = MyBeanFactory.createPerson();
person.add();
person.update();
person.delete();
} }
spring的cglib代理的更多相关文章
- Spring强制使用CGLIB代理事务
Spring强制使用CGLIB代理事务 springaopjdkreferenceclasspath Spring1.2: 将事务代理工厂[TransactionProxyFactoryBean] ...
- (转)Java动态代理与CGLib代理
<br>public class UserDAOImpl{ <br><br> public void save() { <br> / ...
- 基于Spring AOP的JDK动态代理和CGLIB代理
一.AOP的概念 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的 ...
- Spring框架_代理模式(静态代理,动态代理,cglib代理)
共性问题: 1. 服务器启动报错,什么原因? * jar包缺少.jar包冲突 1) 先检查项目中是否缺少jar包引用 2) 服务器: 检查jar包有没有发布到服务器下: ...
- jdk动态代理与cglib代理、spring aop代理实现原理
原创声明:本博客来源与本人另一博客[http://blog.csdn.net/liaohaojian/article/details/63683317]原创作品,绝非他处摘取 代理(proxy)的定义 ...
- jdk动态代理与cglib代理、spring aop代理实现原理解析
原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ...
- 何为代理?jdk动态代理与cglib代理、spring Aop代理原理浅析
原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ...
- spring中基于JDK和CGLIB代理在项目的应用
一.环境与问题 环境 spring boot的版本是1.2.1.RELEASE.JDK版本是1.7 问题 A服务 PeopleService 调用B服务 HelloService ,其中B服务的方 ...
- Spring AOP demo 和获取被CGLIB代理的对象
本文分为两部分:1)给出Spring AOP的一个例子(会使用CGLIB代理):2)给出获取被CGLIB代理的原始对象. 1.Spring AOP Demo 这部分参考了博文(http://www.v ...
随机推荐
- 编写高质量代码--改善python程序的建议(一)
原文发表在我的博客主页,转载请注明出处! 初衷 python是一个入门十分容易的编程语言,但是想要写好python却是一件不容易的事情,如果不是专业使用python的人,只是将python作为一个脚本 ...
- freemarker的${!}
${sss!} <#--没有定义这个变量,默认值是空字符串! --> ...................................... 转自:https://blog.csdn ...
- J2EE知识体系(简单整理)
- Contos更换python版本
1.查看版本 #python -VPython 2.6.6 2.安装前准备,安装相关库#yum install gcc gcc-c++ autoconf automake#yum install op ...
- curl post CURLOPT_POSTFIELDS
PHP: curl_setopt - Manual http://php.net/manual/en/function.curl-setopt.php CURLOPT_POST TRUE to do ...
- Java 多线程通信之多生产者/多消费者
// 以生产和消费烤鸭为例 class Resource { private String name; private int count = 1; // 记录烤鸭的编号 private boolea ...
- shell脚本杂
1.sh -x 跟踪shell脚本中的每个命令 [root@master shellexer]# cat bash.sh #!/bin/bash var=$ echo $var [root@maste ...
- C++设计模式 -- 解析和实现
原文地址 http://c.chinaitlab.com/special/sjms/Index.html#a 导航目录 ※ 设计模式解析和实现之一-Factory模式 ※ 设计模式解析和实现之八-C ...
- MySQL(单表的表记录的操作)
一.表记录的增删改查 1.增加表记录 <1>插入一条记录: insert [into] tab_name (field1,filed2,.......) values (value1,va ...
- mysql聚合函数操作
1.mysql对中文进行排序 注:是用convert函数用gb2312编码转换 SELECT * FROM 表名 ORDER BY CONVERT(字段名 USING gb2312 ) ASC;