Created
September 14, 2019 15:13
-
-
Save BohdanLevchenko/f84ebe9b77162c4a618c5bece55a84c8 to your computer and use it in GitHub Desktop.
proxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.lang.reflect.InvocationHandler; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Proxy; | |
public class Main { | |
static class Worker implements IWorker { | |
public void doSomething() { | |
System.out.println("doing something"); | |
} | |
} | |
public static void main(String[] args) { | |
final Worker worker = new Worker(); | |
IWorker proxy = (IWorker) Proxy.newProxyInstance(Main.class.getClassLoader(), new Class[]{IWorker.class}, new InvocationHandler() { | |
@Override | |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | |
System.out.println("-- open tx"); | |
try { | |
return method.invoke(worker, args); | |
} finally { | |
System.out.println("-- close tx"); | |
} | |
} | |
}); | |
proxy.doSomething(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment