自定义一个接口 和一个接实现类
public interface Hello {
void setInfo(String a,String b);
String getInfo();
void hah(String s);
}
/**
* Created by likailong on 2016/9/29.
*/
public class HelloIml implements Hello {
private String a;
private String b;
public void setInfo(String a, String b){
this.a=a;
this.b=b;
}
@Override
public String getInfo() {
return a;
}
public void hah(String s){
System.out.print("ssss"+s);
}
}
package cn.itcast.shujujiegou.StructuresAnalysis;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
* Created by likailong on 2016/9/29.
*/
public class AopHandler implements InvocationHandler {
private Object target;
public AopHandler(Object target){
this.target=target;
}
public void println(String str,Object...args){
System.out.println(str);
if(args==null){
System.out.println("没有传递任何值");
}else{
for(Object obj:args){
System.out.println(obj);
}
}
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("\n\n======调用方法名"+method.getName());
Class<?>[] variables=method.getParameterTypes();
System.out.println("\n\t 参数列表:\n");
for(Class<?> typevariables:variables){
System.out.println("\t\t\t"+typevariables.getName());
}
println("\n\n\t 传入参数值为:\n");
for(Object arg:args){
System.out.println("\t\t\t"+arg);
}
Object result=method.invoke(target,args);
println("返回的参数为",result);
println("返回的类型为",method.getReturnType());
return result;
}
}
动态代理的实现如上代码,这只是实现的一种。下面测试上面的代码
public class MethodInvokeSample {
public static void main(String [] args) throws Exception {
Method method=MethodInvokeSample.class.getDeclaredMethod("test",String.class,int.class);
String result=(String)method.invoke(null,"fuck",21);
System.out.println(result);
}
public static String test(String a,int b){
return "传入参数:"+a+"传入参数:"+b;
}
}
结果
动态将方法执行并且传入参数。但是上面没有展现出面向切面只是动态传入了一个值下面自定义BeanFactory
package cn.itcast.shujujiegou.StructuresAnalysis;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
/**
* Created by likailong on 2016/9/29.
*/
public class BeanFactory {
public static Object getBean(String classname) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Object obj=Class.forName(classname).newInstance();
InvocationHandler handle = new AopHandler(obj);
return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),handle);
}
public static<T> T getBean(String classname,Class<T> clazz) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
return (T)getBean(classname);
}
}
测试
package cn.itcast.shujujiegou.StructuresAnalysis;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Created by likailong on 2016/9/29.
*/
public class BeanfatoryTest {
public static void main(String [] args) throws IllegalAccessException, ClassNotFoundException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Hello aa = BeanFactory.getBean("cn.itcast.shujujiegou.StructuresAnalysis.HelloIml", HelloIml.class);
aa.setInfo("hjaslg","sldkjgs");
Method method = Hello.class.getDeclaredMethod("setInfo", String.class, String.class);
method.invoke(new HelloIml(),"aaa","bbb");
}
}
分析newinstance实际原理
从图看出hello接口是代理指向因此代理对象与实现类无关用了字节码增强技术,故代理对象指向接口