Last active
August 6, 2016 11:34
-
-
Save mjedynak/f9d74bb638ca3f7132eab8a564e9b034 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 LazyInitWithProxyThreadSafeServiceFactory { | |
public static Service createService() { | |
return Reflection.newProxy(Service.class, new ThreadSafeLazyInitInvocationHandler()); | |
} | |
private static class ThreadSafeLazyInitInvocationHandler extends AbstractInvocationHandler { | |
private static final LazyInitializer<Service> initializer = new LazyInitializer<Service>() { | |
@Override | |
protected Service initialize() { | |
return new DefaultService(); | |
} | |
}; | |
@Override | |
protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable { | |
return method.invoke(initializer.get(), args); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment