1.静态代理

主题对象:Student

public interface Student {

    public String add();
}

目标对象:RealStudent

public class RealStudent implements Student {
public String add() {
System.out.println("add Ok");
return null;
}
}

代理对象:ProxStudent

public class ProxStudent implements Student {
//对象间交互
private Student realStudent; public Student getRealStudent() {
return realStudent;
} public void setRealStudent(Student realStudent) {
this.realStudent = realStudent;
this.realStudent=realStudent;
} public String add() {
System.out.println("事务已经开启!");
return realStudent.add(); }
}

单测

//静态代理
@Test
public void test01(){
//真实主题对象
Student student=new RealStudent();
//代理对象
ProxStudent proxStudent=new ProxStudent(); proxStudent.setRealStudent(student); proxStudent.add(); }

2.JDK动态代理

接口:IUserDAO

public interface IUserDAO {
public String add(); public String edit();
}

类:UserDAOImpl

public class UserDAOImpl implements IUserDAO {
public String add() {
System.out.println("add");
return "add";
} public String edit() {
System.out.println("edit");
return "edit";
}
}

单测

//JDK动态代理
@Test
public void test02(){
final IUserDAO dao=new UserDAOImpl();
IUserDAO proxy=(IUserDAO) Proxy.newProxyInstance(dao.getClass().getClassLoader(), dao.getClass().getInterfaces(), new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("事务已经开启");
method.invoke(dao,args);
return null;
}
});
//代理对象 add() edit()
proxy.add();
proxy.edit(); }

3.CGLIB动态代理

public class UserService {
public void dalete() {
System.out.println("dalete OK");
}
}

单测 方式一

//CGLIB 动态代理 一
@Test
public void test03(){ final UserService service=new UserService(); Enhancer enhancer =new Enhancer();
//在内存中构建业务类的子类
enhancer.setSuperclass(service.getClass());
enhancer.setCallback(new org.springframework.cglib.proxy.InvocationHandler() {
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
System.out.println("事务已经开启");
method.invoke(service,objects);
return null;
}
});
UserService proxy=(UserService)enhancer.create();
proxy.dalete();
}

单测 方式二

//CGLIB 动态代理 二
@Test
public void test04(){
final UserService service=new UserService(); Enhancer enhancer =new Enhancer();
//在内存中构建业务类的子类
enhancer.setSuperclass(service.getClass());
enhancer.setCallback(new MethodInterceptor() {
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("事务已经开启");
methodProxy.invoke(service,objects);
return null;
}
});
UserService proxy=(UserService)enhancer.create();
proxy.dalete(); }

SPRING代理模式的更多相关文章

  1. Spring代理模式及AOP基本术语

    一.代理模式: 静态代理.动态代理 动态代理和静态代理区别?? 解析:静态代理需要手工编写代理类,代理类引用被代理对象. 动态代理是在内存中构建的,不需要手动编写代理类 代理的目的:是为了在原有的方法 ...

  2. Spring 代理模式及AOP基本术语

    一.代理模式: 静态代理.动态代理 动态代理和静态代理区别?? 解析:静态代理需要手工编写代理类,代理类引用被代理对象. 动态代理是在内存中构建的,不需要手动编写代理类 代理的目的:是为了在原有的方法 ...

  3. Spring代理模式(jdk动态代理模式)

    有动态代理和静态代理: 静态代理就是普通的Java继承调用方法. Spring有俩种动态代理模式:jdk动态代理模式 和 CGLIB动态代理 jdk动态代理模式: 代码实现: 房东出租房子的方法(继承 ...

  4. Spring代理模式(CGLIB动态代理模式)

    jdk动态代理和CGLIB动态代理 没什么太大的区别,CGLIB动态代理不需要接口,但是需要导入jar包. 房东出租房子的方法: package com.bjsxt.proxy2; public cl ...

  5. spring代理模式 service远程调用,插件执行

    最近,研究了一下平台远程调用的过程,和service层插件执行的原理,记录一下. 1.远程service调用过程 首先看一下类的继承结构 封装调用处理过程 封装service调用接口 封装servic ...

  6. Spring AOP /代理模式/事务管理/读写分离/多数据源管理

    参考文章: http://www.cnblogs.com/MOBIN/p/5597215.html http://www.cnblogs.com/fenglie/articles/4097759.ht ...

  7. 代理模式及其在spring与struts2中的体现

    代理模式 代理模式有三个角色组成: 1.抽象主题角色:声明了真实主题和代理主题的共同接口. 2.代理主题角色:内部包含对真实主题的引用,并且提供和真实主题角色相同的接口. 3.真实主题角色:定义真实的 ...

  8. Spring框架_代理模式(静态代理,动态代理,cglib代理)

    共性问题: 1. 服务器启动报错,什么原因? * jar包缺少.jar包冲突 1) 先检查项目中是否缺少jar包引用 2) 服务器: 检查jar包有没有发布到服务器下:                 ...

  9. spring设计模式_代理模式

    代理模式应该是Spring核心设计模式之一了 先说下代理模式特性: 1.有代理人和被代理人 2.对于被代理的人来说,这件事情是一定要做的,但是我又不想做,所有就找代理人来做. 3.需要获取到被代理人的 ...

随机推荐

  1. 使用ubuntu自带的Remmina Remote Desktop Client远程登录服务器配置

    1.配置:点击new , 配置服务器ip地址.名称.密码 2.打开本机终端执行一下命令: echo xfce4-session>.session echo xfce4-session>.x ...

  2. java.sql.SQLException: Operation not allowed after ResultSet closed

    转自:http://blog.csdn.net/hellobobantang/article/details/7173622 java.sql.SQLException: Operation not ...

  3. 字典树Trie的使用

    1. Trie树介绍 Trie,又称单词查找树.前缀树,是一种多叉树结构.如下图所示: 上图是一棵Trie树,表示了关键字集合{“a”, “to”, “tea”, “ted”, “ten”, “i”, ...

  4. AtCoder Grand Contest 028 A:Two Abbreviations

    题目传送门:https://agc028.contest.atcoder.jp/tasks/agc028_a 题目翻译 给你两个串\(s\)与\(t\),长度分别为\(n,m\).问你存不存在一个串长 ...

  5. poj2299——逆序对

    题目:http://poj.org/problem?id=2299 逆序对,注意树状数组维护后缀和. 代码如下: #include<iostream> #include<cstdio ...

  6. C#中使用GetCursorPos获取屏幕坐标

    [StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; public POINT ...

  7. Poj_1068 Parencodings

    S     (((( )( )() ) ) ) P-sequence     4 5 6666,表示第i个右括号的左边有几个左括号. W-sequence    1 1 1456,表示第i个右括号和以 ...

  8. Ubuntu——查看内存和CPU情况

    查看内存及cpu使用情况的命令:top 也可以安装htop工具,这样更直观,安装命令如下:sudo apt-get install htop安装完后,直接输入命令:htop

  9. Windows WMIC命令使用详解1

    https://blog.csdn.net/enweitech/article/details/51982114 在CMD和Powershell中 使用WMIC 先决条件: a. 启动Windows ...

  10. typedef 函数指针的用法

    转自:http://www.cnblogs.com/shenlian/archive/2011/05/21/2053149.html typedef 函数指针的用法 在网上搜索函数指针,看到一个例子. ...