Advice接口
import java.lang.reflect.Method;
public interface Advice
{
public void beforProxy(Method method);
public void afterProxy(Method method);
}
Myadvice
import java.lang.reflect.Method;
public class MyAdvice implements Advice
{
long beginTime = 0;
long endTime = 0;
@Override
public void beforProxy(Method method)
{
beginTime = System.currentTimeMillis();
}
@Override
public void afterProxy(Method method)
{
endTime = System.currentTimeMillis();
System.out.println(endTime - beginTime);
}
}
ProxyFactory
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyFactory
{
private Object target;
private Advice advice;
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;
}
public Object getProxy()
{
Object proxys = Proxy.newProxyInstance(target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler()
{
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
advice.beforProxy(method);
Object reval = method.invoke(target, args);
advice.afterProxy(method);
return reval;
}
});
return proxys;
}
}
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class BeanFactoryTest
{
Properties properties = new Properties();
public BeanFactoryTest(InputStream inStream)
{
try
{
properties.load(inStream);
} catch (IOException e)
{
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
}
public Object getBean(String name)
{
String className = properties.getProperty(name);
Object bean = null;
try
{
Class clazz = Class.forName(className);
bean = clazz.newInstance();
if(bean instanceof ProxyFactory)
{
ProxyFactory proxyFactoryBean = (ProxyFactory) bean;
Advice advice = (Advice) Class.forName(properties.getProperty(name + ".advice")).newInstance();
Object target = (Object) Class.forName(properties.getProperty(name + ".target")).newInstance();
proxyFactoryBean.setAdvice(advice);
proxyFactoryBean.setTarget(target);
Object proxy = proxyFactoryBean.getProxy();
return proxy;
}
} catch (Exception e)
{
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
return bean;
}
}
BeanFactoryTest
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class BeanFactoryTest
{
Properties properties = new Properties();
public BeanFactoryTest(InputStream inStream)
{
try
{
properties.load(inStream);
} catch (IOException e)
{
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
}
public Object getBean(String name)
{
String className = properties.getProperty(name);
Object bean = null;
try
{
Class clazz = Class.forName(className);
bean = clazz.newInstance();
if(bean instanceof ProxyFactory)
{
ProxyFactory proxyFactoryBean = (ProxyFactory) bean;
Advice advice = (Advice) Class.forName(properties.getProperty(name + ".advice")).newInstance();
Object target = (Object) Class.forName(properties.getProperty(name + ".target")).newInstance();
proxyFactoryBean.setAdvice(advice);
proxyFactoryBean.setTarget(target);
Object proxy = proxyFactoryBean.getProxy();
return proxy;
}
} catch (Exception e)
{
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
return bean;
}
}
Test
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class test
{
public static void main(String[] args) throws FileNotFoundException
{
InputStream inputStream = new FileInputStream("src\\ReflectTest.properties");
BeanFactoryTest beanFactoryTest = new BeanFactoryTest(inputStream);
Object bean = beanFactoryTest.getBean("xxx");
System.out.println(bean.getClass().getName());
}
}
ReflectTest.properties配置文件
xxx = java.util.ArrayList
#xxx = ProxyBeanFactory.ProxyFactory
xxx.advice = ProxyBeanFactory.MyAdvice
xxx.target = java.util.ArrayList