JAVA AOP 动态代理 的例子
来源:互联网 发布:微团购软件哪个好 编辑:程序博客网 时间:2024/05/17 00:54
package myaop;
import java.util.*;
import java.lang.reflect.*;
interface person {
String getName();
int getID();
}
class student implements person{
private String name;
private int id;
student(){
System.out.println("student: student() called ");
name="nonmae";
id=0;
}
student(int mid, String name){
this.name=name;
this.id=mid;
}
public String getName(){
System.out.println("student: getName() called. Name= "+name);
return name;
}
public int getID() {
System.out.println("student: getID() called: id= "+id);
return id;
}
}
class actions {
public void BeforeActions(){
System.out.println("actions: BeforeActions() called ");
}
public void AfterActions(){
System.out.println("actions: AfterActions() called ");
}
}
class myAOP implements InvocationHandler{
public Object proxyee;
public void setTarget(Object target){
proxyee=target;
}
public Object invoke(Object myproxy, Method method, Object[] args){
System.out.println("myAOP:invoke() enters");
actions maction = new actions();
maction.BeforeActions();
Object result=null;
try {
result=method.invoke(proxyee,args);
}
catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
maction.AfterActions();
return result;
}
}
public class AOPTrial {
public static void main(String[] args) {
// TODO Auto-generated method stub
person sta = new student(1,"jac2k");
myAOP handler = new myAOP();
handler.setTarget(sta);
Object mystu=Proxy.newProxyInstance(sta.getClass().getClassLoader(),
sta.getClass().getInterfaces(),handler);
/*
* student: getID() called: id= 1
* student: getName() called. Name= jac2k
*/
((person)sta).getID();
((person)sta).getName();
/*
myAOP:invoke() enters
actions: BeforeActions() called
student: getID() called: id= 1
actions: AfterActions() called
myAOP:invoke() enters
actions: BeforeActions() called
student: getName() called. Name= jac2k
actions: AfterActions() called
*/
//here can't covert to student
((person)mystu).getID();
((person)mystu).getName();
}
import java.util.*;
import java.lang.reflect.*;
interface person {
String getName();
int getID();
}
class student implements person{
private String name;
private int id;
student(){
System.out.println("student: student() called ");
name="nonmae";
id=0;
}
student(int mid, String name){
this.name=name;
this.id=mid;
}
public String getName(){
System.out.println("student: getName() called. Name= "+name);
return name;
}
public int getID() {
System.out.println("student: getID() called: id= "+id);
return id;
}
}
class actions {
public void BeforeActions(){
System.out.println("actions: BeforeActions() called ");
}
public void AfterActions(){
System.out.println("actions: AfterActions() called ");
}
}
class myAOP implements InvocationHandler{
public Object proxyee;
public void setTarget(Object target){
proxyee=target;
}
public Object invoke(Object myproxy, Method method, Object[] args){
System.out.println("myAOP:invoke() enters");
actions maction = new actions();
maction.BeforeActions();
Object result=null;
try {
result=method.invoke(proxyee,args);
}
catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
maction.AfterActions();
return result;
}
}
public class AOPTrial {
public static void main(String[] args) {
// TODO Auto-generated method stub
person sta = new student(1,"jac2k");
myAOP handler = new myAOP();
handler.setTarget(sta);
Object mystu=Proxy.newProxyInstance(sta.getClass().getClassLoader(),
sta.getClass().getInterfaces(),handler);
/*
* student: getID() called: id= 1
* student: getName() called. Name= jac2k
*/
((person)sta).getID();
((person)sta).getName();
/*
myAOP:invoke() enters
actions: BeforeActions() called
student: getID() called: id= 1
actions: AfterActions() called
myAOP:invoke() enters
actions: BeforeActions() called
student: getName() called. Name= jac2k
actions: AfterActions() called
*/
//here can't covert to student
((person)mystu).getID();
((person)mystu).getName();
}
}
输出结果
student: getID() called: id= 1
student: getName() called. Name= jac2k
myAOP:invoke() enters
actions: BeforeActions() called
student: getID() called: id= 1
actions: AfterActions() called
myAOP:invoke() enters
actions: BeforeActions() called
student: getName() called. Name= jac2k
actions: AfterActions() called
0 0
- JAVA AOP 动态代理 的例子
- JAVA动态代理实现aop的简单例子
- java的动态代理(AOP)
- String Aop 动态代理例子
- Spring AOP动态代理例子
- Java动态代理模拟spring的AOP
- (spring AOP)VS(动态代理的例子)
- java学习之路----静态代理---动态代理-----AOP的前奏(AOP也是动态代理)
- Java 动态代理的实现-代理模式--aop
- java 动态代理 例子
- Java 例子 反射实现 AOP 动态代理模式(Spring Aop实现原理)
- java动态代理的一个例子
- JAVA动态代理实现的小例子
- java自身的动态代理简单例子。
- java动态代理--一个简单的例子
- Java动态代理实现AOP
- java动态代理实现aop
- java动态代理实现AOP
- CSS学习01
- JVM飙高排查脚本-结构分析
- Runtime.getRuntime().exec方法
- 吉哥系列故事——临时工计划
- 基于飞凌2440开发板rt3070工作在AP模式 ( 二)
- JAVA AOP 动态代理 的例子
- Object-C--->Swift之(六)函数入门级讲解
- hdu 1403
- 向每一个错误致敬——Android HotFix
- 指针定义字符串和数组定义字符串的区别
- 小明系列故事——买年货(三重背包)
- 2015——程序人生进阶
- 【Android开发小记--3】按钮视图动画几种效果
- UESTC--1264--人民币的构造(数学规律)