package casic.com.base.aoptest;
import java.io.InputStream;
import java.util.Properties;
import casic.com.base.proxytest.Advice;
public class BeanFactory {
Properties props = new Properties();
public BeanFactory(InputStream ips){
try {
props.load(ips);
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 为了便于阅读就不处理异常了
*/
public Object getBean(String name) throws Exception {
String className = props.getProperty(name);
Class clazz =Class.forName(className);
//对于javabean来说必须要有一个无参的构造器
Object bean = clazz.newInstance();
if(bean instanceof ProxyFactoryBean) {
ProxyFactoryBean proxyFactoryBean = (ProxyFactoryBean) bean;
Advice advice =(Advice) Class.forName((String) props.get(name + ".advice")).newInstance();
Object target =Class.forName((String) props.get(name + ".target")).newInstance();
proxyFactoryBean.setAdvice(advice);
proxyFactoryBean.setTarget(target);
Object proxy = ((ProxyFactoryBean)bean).getProxy();
return proxy;
}
return bean;
}
}
package casic.com.base.aoptest;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import casic.com.base.proxytest.Advice;
public class ProxyFactoryBean {
private Advice advice;
private Object target;
public Object getProxy() {
Object proxy = Proxy.newProxyInstance(
target.getClass().getClassLoader(),//调用目标类的加载器加载代理类
target.getClass().getInterfaces(),//获得目标类的实现的方法(同时也是代理类实现的方法),当代理类调用这些方法时,会调用代理类的invoke方法。
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
advice.beforeMethod(method);
Object retVal = method.invoke(target, args);
System.out.println("调用目标方法"+method.getName());
advice.afterMethod(method);
return retVal;
}
});
return proxy;
}
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;
}
}
package casic.com.base.aoptest;
public class Person implements SayHello{
@Override
public String sayHello() {
return "hello,world";
}
}
package casic.com.base.aoptest;
public interface SayHello {
String sayHello();
}
package casic.com.base.aoptest;
import java.io.InputStream;
import java.util.List;
public class AopTest {
public static void main(String[] args) throws Exception {
InputStream ips = AopTest.class.getResourceAsStream("config.properties");
List bean = (List) new BeanFactory(ips).getBean("xxx");
bean.add("1");
System.out.println(bean.getClass().getName());
InputStream ips2 = AopTest.class.getResourceAsStream("config.properties");
SayHello person = (SayHello) new BeanFactory(ips2).getBean("person");
person.sayHello();
System.out.println(person.getClass().getName());
}
}
package casic.com.base.proxytest;
import java.lang.reflect.Method;
/**
* 通信的契约
*
* 通告 建议
* Copyright © 2018 eSunny Info. Tech Ltd. All rights reserved.
* @Package: casic.com.base.proxytest
* @author: lh
* @date: 2018年5月2日 下午10:08:52
*/
public interface Advice {
void beforeMethod(Method method);
void afterMethod(Method method);
}
package casic.com.base.proxytest;
import java.lang.reflect.Method;
public class MyAdvice implements Advice{
long beginTime;
@Override
public void beforeMethod(Method method) {
System.out.println("目标方法执行之前执行。。。");
beginTime = System.currentTimeMillis();
}
@Override
public void afterMethod(Method method) {
System.out.println("目标方法执行结束之后执行。。。");
long endTime = System.currentTimeMillis();
System.out.println(method.getName() +"运行时间为" + (endTime - beginTime));
}
}
动态代理不理解点这里