1.项目分层图

简单的JDK动态代理模式


2.Employer的代码:

package com.hhwy.service

public interface IEmployer{

void addEmployer();  //在接口中定义添加雇员方法

}

3.EmployerImp的代码:

package com.hhwy.service;
public class EmployerImpl implements IEmployer{
@Override
public void addEmployer(){
System.out.println("加入北京有限公司就从现在做起!");
}

}

4.MyInvocation的代码:

package com.hhwy.service;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class MyInvocation implements InvocationHandler{
private Object target;
@Override
public Object invoke(Object proxy, Method method, Object[] args)throws Throwable{
System.out.println("对象执行之前的代码逻辑");
Object resultObject=method.invoke(target, args);
System.out.println("对象执行之后的代码逻辑");
return resultObject;
}
public MyInvocation(Object target){
this.target=target;
}

}

5.Test的方法:

简单的JDK动态代理模式

相关文章:

  • 2021-09-02
  • 2022-01-04
  • 2021-07-23
  • 2021-10-05
  • 2022-12-23
  • 2021-10-12
  • 2022-02-09
  • 2021-07-17
猜你喜欢
  • 2021-10-10
  • 2021-07-26
  • 2021-11-05
  • 2021-08-15
  • 2021-06-29
  • 2022-02-01
相关资源
相似解决方案