public class ProxyFacotryBean {
public Object getBean(final Object target,final Advice advice)
{
Object bean=Proxy.newProxyInstance(
Collection.class.getClassLoader(),
Collection.class.getInterfaces(),
new InvocationHandler()
{
@Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
// TODO Auto-generated method stub
advice.before(method);
Object vel=method.invoke(target, args);
advice.after(method);
return vel;
}
});
return bean;
}
}
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class BeanFactory {
private Properties prp=new Properties();
public BeanFactory(InputStream inStream)
{
try {
prp.load(inStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Object getBean(String name)
{
Object bean=null;
String className=prp.getProperty(name);
try {
bean=Class.forName(className).newInstance();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(bean instanceof ProxyFacotryBean)
{
Object target;
try {
target = Class.forName(prp.getProperty(name+".target")).newInstance();
Advice advice=(Advice)Class.forName(prp.getProperty(name+".advice")).newInstance();
bean=new ProxyFacotryBean().getBean(target, advice);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bean;
}
}
|
|