Created
August 6, 2016 11:23
-
-
Save mjedynak/eb92c40df625ae52950c0dadb4b56268 to your computer and use it in GitHub Desktop.
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
public class LazyInitWithProxyServiceFactory { | |
public static Service createService() { | |
return Reflection.newProxy(Service.class, new LazyInitInvocationHandler()); | |
} | |
private static class LazyInitInvocationHandler extends AbstractInvocationHandler { | |
private Service service; | |
@Override | |
protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable { | |
return method.invoke(service(), args); | |
} | |
private Service service() { | |
if (service == null) { | |
service = new DefaultService(); | |
} | |
return service; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment