Java 动态代理模仿Spring AOP

Advice切面接口


package aop;

import java.lang.reflect.Method;

public interface Advice {

	void beforeMethed(Method method);
	
	void afterMethed(Method method);
}


MyAdvice切面实现类


package aop;

import java.lang.reflect.Method;

public class MyAdvice implements Advice {

	private long beginTime=0;
	
	@Override
	public void beforeMethed(Method method) {
		beginTime=System.currentTimeMillis();
		System.out.println("before method:"+method.getName());
		
	}

	@Override
	public void afterMethed(Method method) {

		long endTime=System.currentTimeMillis();
		System.out.println("end method:"+method.getName()+" method use "+String.valueOf(endTime-beginTime)+" millisecond");
	}

}


代理工厂


package aop;

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

public class ProxFactoryBean {

	private Object target;
	private Advice advice;
	
	
	public Object getProx(){
		
		Object prox= Proxy.newProxyInstance(
				target.getClass().getClassLoader(),//类加载器 
				target.getClass().getInterfaces(), //目标对象的接口
				new InvocationHandler() {
					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						advice.beforeMethed(method);//调用目标之前
						Object resVal= method.invoke(target, args);//调用目标
						advice.afterMethed(method);//调用目标之后
						return resVal;
					}
				});
		return prox;//这里放回的是目标对象的接口,所以调用的时候也要用接口
	}
	
	
	
	
	
	public void setTarget(Object target) {
		this.target = target;
	}
	public void setAdvice(Advice advice) {
		this.advice = advice;
	}
}

Bean工厂


package aop;

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

public class BeanFactory {

	private Properties properties=new Properties();
	
	public BeanFactory(InputStream inStream) throws IOException {
		properties.load(inStream);
	}
	
	public Object getBean(String beanName) {
		
		String className=properties.getProperty(beanName);//到配置文件中查找bean的全路径
		Object bean=null;
		
		try {
			bean = Class.forName(className).newInstance();//实例化Bean
		} catch (Exception e) {
			e.printStackTrace();
		} 
				
		if(bean instanceof ProxFactoryBean){//如果Bean是代理对象
			ProxFactoryBean proxFactoryBean=(ProxFactoryBean)bean;//获取代理对象
			try {
				Advice advice = (Advice) Class.forName(properties.getProperty(beanName +".advice")).newInstance();
				Object target=Class.forName(properties.getProperty(beanName +".target")).newInstance();
				
				proxFactoryBean.setAdvice(advice);//设置切面
				proxFactoryBean.setTarget(target);//设置代理那个对象
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return proxFactoryBean.getProx();
		}
		
		return bean;
	}
}

配置文件


#xxx=java.util.ArrayList
xxx=aop.ProxFactoryBean
xxx.advice=aop.MyAdvice
xxx.target=java.util.ArrayList


测试类


package aop;

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

public class AopMain {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		BeanFactory factory=new BeanFactory(
				AopMain.class.getResourceAsStream(
						"bean.properties"));
		
		Collection list=(Collection) factory.getBean("xxx");//这里要用接口
		list.add("a");
		System.out.println(list.size());
	}

}



你可能感兴趣的:(Java 动态代理模仿Spring AOP)