package again.aopframework;
import java.io.InputStream;
import java.util.Collection;
public class AopFrameworkTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*这个总程序的思想是:
如果xxx是java.util.ArrayList 就直接返回其实例
如果xxx是again.aopframework.MyAdvice就返回其java.util.ArrayList的代理类*/
InputStream ips=AopFrameworkTest.class.getResourceAsStream("config.properties");
Object obj=new BeanFactory(ips).getBean("xxx");
System.out.println(obj.getClass().getName());
Collection col=(Collection)obj;
col.add("ffff");
System.out.println(col.size());
}
}
|
package again.aopframework;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class BeanFactory {
Properties props=new Properties();
public BeanFactory(InputStream ips){
try {
props.load(ips);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Object getBean(String name){
String className=props.getProperty(name);
Object bean=null;
try {
Class clazz=Class.forName(className);
bean=clazz.newInstance();//JavaBean 必须要有一个不带参数的构造方法
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(bean instanceof ProxyFactoryBean){
Object proxy=null;
ProxyFactoryBean proxyFactoryBean=(ProxyFactoryBean)bean;
try{
Advice advice=(Advice)Class.forName(props.getProperty(name+".advice")).newInstance();
Object target=Class.forName(props.getProperty(name+".target")).newInstance();
proxyFactoryBean.setAdvice(advice);
proxyFactoryBean.setTarget(target);
proxy=proxyFactoryBean.getProxy(target,advice);
}
catch(Exception e){
e.printStackTrace();
}
return proxy;
}
return bean;
}
}
|
package again.aopframework;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
class ProxyFactoryBean {
private Object target;
private Advice advice;
public Object getProxy(final Object target,final Advice advice){
Object proxy=Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler(){
@Override
public Object invoke(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
// TODO Auto-generated method stub
advice.beforeMethod(arg1);
Object retVal=arg1.invoke(target, arg2);
advice.afterMethod(arg1);
return retVal;
}
}
);
return proxy;
}
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;
}
}
|
package again.aopframework;
import java.lang.reflect.Method;
class MyAdvice implements Advice {
long beginTime;
@Override
public void beforeMethod(Method method) {
// TODO Auto-generated method stub
beginTime=System.currentTimeMillis();
}
@Override
public void afterMethod(Method method) {
// TODO Auto-generated method stub
long endTime=System.currentTimeMillis();
System.out.println("耗时:"+(endTime-beginTime));
}
}
|
package again.aopframework;
import java.lang.reflect.Method;
interface Advice {
void beforeMethod(Method method);
void afterMethod(Method method);
}
|
#xxx=java.util.ArrayList
xxx=again.aopframework.ProxyFactoryBean
xxx.advice=again.aopframework.MyAdvice
xxx.target=java.util.ArrayList