net.dongliu.commons.async.LazyService Maven / Gradle / Ivy
package net.dongliu.commons.async;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.function.Supplier;
/**
* Wrapper service to lazy create
*
* @author Liu Dong
*/
public class LazyService {
@SuppressWarnings("unchecked")
public static T create(Class interfaceClass, Supplier supplier) {
Lazy lazy = Lazy.create(supplier);
InvocationHandler ih = (proxy, method, args) -> method.invoke(lazy.get(), args);
T service = (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[]{interfaceClass}, ih);
return service;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy