JAVA的动态代理,在MYBATIS中应用的很广,其核心就是写一个interface,但不写实现类,而后用动态代理来实例化并执行这个interface中的方法,话很少说,来看一个实现的例子:java
1.先定义一个接口:ide
public interface TestProxy { String hello(); }
2.虽然不写实现类,但咱们仍然但愿在执行这个hello()方法时,能输出咱们想要输出的内容,好比我把但愿要输出的内容放在一个属性文件中:测试
hello=world
我但愿在调用hello方法时,输出world,接下来得解析这个属性文件:this
public class PropertiesHandler { private static Properties p = new Properties(); static{ try { InputStream in = new FileInputStream(new File("src/test.properties")); p.load(in); in.close(); } catch (IOException e) { throw new RuntimeException("test.properties load error!"); } } public static String getProperty(String key){ try{ return p.getProperty(key, null); }catch(Exception e){ e.printStackTrace(); } return ""; } }
3.解析完后,咱们再写一个动态代理类:代理
public class ProxyImp<T> implements InvocationHandler{ private Class<T> proxyMethod; public ProxyImp(Class<T> proxyMethod) { this.proxyMethod = proxyMethod; } @Override public Object invoke(Object proxy, Method method, Object[] args)throws Throwable { String value = PropertiesHandler.getProperty(method.getName()); System.out.println(value); return null; } }
其原理就是在调用接口类方法时,动态代理类都会去执行这个invoke方法,以达到执行的目的,能够看到,在invoke方法里,咱们把hello方法在属性文件中对应的值给取出来了,并输出。对象
4.接下来就是再封装一个代理工厂类来产生这个接口的一个实例:blog
public class ProxyImpFactory{ @SuppressWarnings("unchecked") public static <T> T newInstance(Class<T> methodInterface) { final ProxyImp<T> proxyImp = new ProxyImp<T>(methodInterface); return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{methodInterface}, proxyImp); } }
能够从上面的代码中看出这个代理类会自动的生成一个接口实现类的实例。接口
5.接下来就是真正的调用了:get
public static void main(String[] args) { TestProxy tp = ProxyImpFactory.newInstance(TestProxy.class); tp.hello(); }
输出就是:world.自动化
6.动态代理在自动化中的做用:
自动化中,当把元素对象用外部文件,把数据文件用外部文件时,均可以用这种方式来进行封装,其在自动化测试中最大的做用,我想就是利用在封装关键字上了,把关键字与具体的方法对应起来,就能够用这样的方式。
至于具体的关键字的实现,我想得靠你们自已了,有兴趣的能够与我讨论!