반사 시 뮬 레이 션 을 통 해 AOP 의 동적 에이전트

3726 단어 JAVA
config.property
#xxx=java.util.ArrayList
xxx=AOPProxy.ProxyFactoryBean
xxx.advice=AOPProxy.Advice
xxx.target=java.util.ArrayList

BeanFactory
package AOPProxy;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


public class BeanFactory {
	Properties pro = new Properties();
	public BeanFactory(InputStream is) throws IOException{
		pro.load(is);
	}
	public Object getBean(String name) throws ClassNotFoundException, InstantiationException, IllegalAccessException{
		Object proxy = null;
		String className = pro.getProperty(name);
		Class cla = Class.forName(className);
		Object bean = (Object)cla.newInstance();
		System.out.println("-----------------");
		
		if(bean instanceof ProxyFactoryBean){
			Advice advice =(Advice) Class.forName((String)pro.get(name +".advice")).newInstance();
			Object target = (Object)Class.forName((String)pro.get(name +".target")).newInstance();
			
			ProxyFactoryBean proxyFactoryBean = (ProxyFactoryBean)bean;
			proxyFactoryBean.setAdvice(advice);
			proxyFactoryBean.setTarget(target);
			proxy = (proxyFactoryBean).getProxy();
			return proxy;
		}
		return bean;
	}
	private Object ProxyFactoryBean(Object bean) {
		// TODO Auto-generated method stub
		return null;
	}
}

ProxyFactoryBean
package AOPProxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


public class ProxyFactoryBean {
	private Advice advice;
	private Object target;
	
	public Advice getAdvice() {
		return advice;
	}

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

	public Object getTarget() {
		return target;
	}

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

	public Object getProxy() {
		// TODO Auto-generated method stub
		Object proxy3 = (Object)Proxy.newProxyInstance(
				target.getClass().getClassLoader(),
				//new Class[]{Collection.class},
				target.getClass().getInterfaces(),
				new InvocationHandler() {
					//     ,  ,  
					@Override
					public Object invoke(Object proxy, Method method, Object[] args)
							throws Throwable {
/*						long bt = System.currentTimeMillis();
						//          
						Object retVal = method.invoke(target, args);
						for (int i = 0; i <10; i++) {
						}
						long et = System.currentTimeMillis();
						System.out.println(method.getName()+" "+ (et - bt )+"hehe");
						return retVal;*/
						
						
						//      
						advice.beforeMethod(method);
						//          
						Object retVal = method.invoke(target, args);
						for (int i = 0; i <10; i++) {
						}
						advice.afterMethod(method);
						return retVal;
					}
				}
		);
		return proxy3;

	}

}

AOPTest
package AOPProxy;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class AopTest {
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
		InputStream is = (InputStream)AopTest.class.getResourceAsStream("config.properties");
		Object bean = (Object)new BeanFactory(is).getBean("xxx");
		System.out.println(bean.getClass().getName());
		//System.out.println(bean.getClass().getName());
	}
}

Advice:
package AOPProxy;
import java.lang.reflect.Method;


public class Advice {
	long bt,et;
	public void beforeMethod(Method method) {
		// TODO Auto-generated method stub
		bt = System.currentTimeMillis();
	}

	public void afterMethod(Method method) {
		// TODO Auto-generated method stub
		et = System.currentTimeMillis();
	}

}

좋은 웹페이지 즐겨찾기