public abstract class AbstractLazyCreationTargetSource extends Object implements TargetSource
TargetSource
implementation that will
lazily create a user-managed object.
Creation of the lazy target object is controlled by the user by implementing
the createObject()
method. This TargetSource
will invoke
this method the first time the proxy is accessed.
Useful when you need to pass a reference to some dependency to an object but you don't actually want the dependency to be created until it is first used. A typical scenario for this is a connection to a remote resource.
isInitialized()
,
createObject()
Modifier and Type | Field and Description |
---|---|
protected Log |
logger
Logger available to subclasses
|
Constructor and Description |
---|
AbstractLazyCreationTargetSource() |
Modifier and Type | Method and Description |
---|---|
protected abstract Object |
createObject()
Subclasses should implement this method to return the lazy initialized object.
|
Object |
getTarget()
Returns the lazy-initialized target object,
creating it on-the-fly if it doesn't exist already.
|
Class<?> |
getTargetClass()
This default implementation returns
null if the
target is null (it is hasn't yet been initialized),
or the target class if the target has already been initialized. |
boolean |
isInitialized()
Return whether the lazy target object of this TargetSource
has already been fetched.
|
boolean |
isStatic()
Will all calls to
TargetSource.getTarget() return the same object? |
void |
releaseTarget(Object target)
Release the given target object obtained from the
TargetSource.getTarget() method, if any. |
protected final Log logger
public boolean isInitialized()
public Class<?> getTargetClass()
null
if the
target is null
(it is hasn't yet been initialized),
or the target class if the target has already been initialized.
Subclasses may wish to override this method in order to provide
a meaningful value when the target is still null
.
getTargetClass
in interface TargetClassAware
getTargetClass
in interface TargetSource
TargetSource
isInitialized()
public boolean isStatic()
TargetSource
TargetSource.getTarget()
return the same object?
In that case, there will be no need to invoke TargetSource.releaseTarget(Object)
,
and the AOP framework can cache the return value of TargetSource.getTarget()
.
isStatic
in interface TargetSource
true
if the target is immutableTargetSource.getTarget()
public Object getTarget() throws Exception
getTarget
in interface TargetSource
null
if there is no actual target instanceException
- if the target object can't be resolvedcreateObject()
public void releaseTarget(Object target) throws Exception
TargetSource
TargetSource.getTarget()
method, if any.releaseTarget
in interface TargetSource
target
- object obtained from a call to TargetSource.getTarget()
Exception
- if the object can't be released