欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

代理模式03

程序员文章站 2022-07-11 12:00:54
...

1、基础类

import java.lang.reflect.Method;

/*系统功能的建议,spring中命名规则*/
public interface Advice {
 public void beforeMethod(Method method);
 public void afterMethod(Method method);
 public String CMethod();
}

import java.lang.reflect.Method;

public class MyAdvice implements Advice {

 long beginTime = 0;
 long endTime = 0;
 public void afterMethod(Method method) {
  System.out.println("结束时间");
   endTime = System.currentTimeMillis();//获取毫秒信息
   System.out.println(CMethod());
 }

 public void beforeMethod(Method method) {
  System.out.println("开始时间");
  beginTime = System.currentTimeMillis();
 }
 
 public String CMethod(){
  return "总共用时" + (beginTime - endTime) ;
 }

}

 

2、代理类

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactoryBean {

 private Object target;
 private Advice advice;

 public Object getProxy() {
  // 创建一个新的代理.
  // InvocationHandler动态方法
  Object proxy = Proxy.newProxyInstance(target.getClass()
    .getClassLoader(), target.getClass().getInterfaces(),
    new InvocationHandler() {
     Object retVal = null;

     public Object invoke(Object obj, Method method,
       Object[] objs) throws Throwable {
      advice.beforeMethod(method);
      retVal = method.invoke(target, objs);
      advice.afterMethod(method);
      return retVal;
     }
    });
  return proxy;
 }

 public String getString() {
  return "testProxyFactoryBean";
 }

 public Object getTarget() {
  return target;
 }

 public void setTarget(Object target) {
  this.target = target;
 }

 public Advice getAdvice() {
  return advice;
 }

 public void setAdvice(Advice advice) {
  this.advice = advice;
 }

}

定义名为config.properties配置文件,其中配置文件.target要代理的文件,.advice为在代理文件前后调用的代理方法,xxx为代理文件(判断是否在工厂进行加工)

#Proxy
#xxx=com.dhy.test.likeSpring.ProxyFactoryBean
xxx = java.util.ArrayList
#xxx=java.util.ArrayList
xxx.advice=com.dhy.test.likeSpring.MyAdvice
xxx.target=java.util.ArrayList

 

 

代理工厂

 

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;

/* Bean工厂用来判定是否进入 AOP切面中,
 * 换句话说就是Bean进入工厂后,经过工厂的处理(其可以选择对进入的信息进行加工,也可以选择不加工)
 * 再输出出来,这就是工厂的概念*/
public class BeanFactory {
 
 Properties properties = new Properties();
 
 public BeanFactory(InputStream inputStream) {
  try {
   properties.load(inputStream);
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 public Object getBean(String name){
  //找到代理Bean的工厂
  String className = properties.getProperty(name);
  Class clazz = null;
  Object bean = null;
  try {
   clazz = Class.forName(className);
   bean = clazz.newInstance();//其就等于new自己
  } catch (Exception e) {
   e.printStackTrace();
  }
  //判断是否是代理工厂类型(没有定义为代理工厂即转出,不对其进行代理)
  if(bean instanceof Collection){
   Object proxy = null;
   ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
   try {
    //由配置文件中获取处理代理的类
    Advice advice = (Advice) Class.forName(properties.get(name + ".advice").toString()).newInstance();
    //获取要代理的类
    Object target = Class.forName(properties.get(name + ".target").toString()).newInstance();
    //设置代理的内部方法
    proxyFactoryBean.setAdvice(advice);
    //设置代理
    proxyFactoryBean.setTarget(target);
    proxy = proxyFactoryBean.getProxy();
   } catch (Exception e) {
    e.printStackTrace();
   }
   return proxy;
  }
  return bean;
 }
 
}