Class LazyInitialization

    • Method Detail

      • createProxy

        public static <T> T createProxy​(Class<T> resultClass,
                                        LazyInitialization.Initializer<? extends T> initializer)
        Creates a proxy that forwards invocations to lazily initialized instance.

        On any method call on resulting proxy it will either initialize instance and cache it, or use existing instance, and delegate the call to it, returning the result or rethrowing exception of the call. There are two exceptions of this rule:

        • Object.finalize() is always ignored when called on proxy
        • Methods from LazyInitialization.Proxy interface are treated specially and handled with separate route. They won't necessarily initialize the proxy and won't be forwarded to the target.

        Proxies will have resultClass as a superclass/superinterface. Additionally, proxy class will implement LazyInitialization.Proxy interface. This wont be reflected in the compile-time type, but it can be checked by instanceof and it can be casted to it.

        Type Parameters:
        T - type of proxy
        Parameters:
        resultClass - class or interface that proxy has to implement. This is also a superclass/superinterface of initialized instance
        initializer - method of initializing actual delegation target
        Returns:
        proxy object