Skip to content

Instantly share code, notes, and snippets.

@mjedynak
Last active August 6, 2016 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjedynak/f9d74bb638ca3f7132eab8a564e9b034 to your computer and use it in GitHub Desktop.
Save mjedynak/f9d74bb638ca3f7132eab8a564e9b034 to your computer and use it in GitHub Desktop.
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