* HelloWorld.java
*/
public
interface HelloWorld {
public int say(String words);
}
* HelloWorldImplements.java
*/
public
class HelloWorldImplements implements HelloWorld {
public int say(String words) {
System.out.println(
"I am saying:"+words);
return
1;
}
}
* HelloWorldHandler.java
*/
public
class HelloWorldHandler implements InvocationHandler {
Object target;
public HelloWorldHandler(Object target) {
super();
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(
"before");
Object result=method.invoke(target,args);
System.out.println(
"after");
return result;
}
}
* Main.java
*/
public
class Main {
public static void main(String[] args) {
HelleWorld helleWorld =
new HelloWorldImplements();
HelloWorldHandler helloWorldHandler =
new HelloWorldHandler(helleWorld);
HelleWorld proxy = (HelleWorld) Proxy.newProxyInstance(helleWorld.getClass().getClassLoader(),
new Class[]{HelleWorld.class}, helloWorldHandler);
proxy.say(
"fuck the world");
}