T
- the type of the value being returned by this variablepublic abstract class LazyContextVariable<T> extends Object implements ILazyContextVariable<T>
Basic abstract implementation for the ILazyContextVariable
interface.
By extending this class instead of directly implementing the ILazyContextVariable
interface,
users can make sure that their variables will be initialized only once (per template execution). Once its
inner abstract loadValue()
method is called --which implementation has to be provided by the user--,
objects of this class will cache the results of such load and return these results every time the
variable value is accessed.
An example:
context.setVariable(
"users",
new LazyContextVariable<List<User>>() {
@Override
protected List<User> loadValue() {
return databaseRepository.findAllUsers();
}
});
Modifier | Constructor and Description |
---|---|
protected |
LazyContextVariable() |
Modifier and Type | Method and Description |
---|---|
T |
getValue()
Returns the variable value.
|
protected abstract T |
loadValue() |
public final T getValue()
ILazyContextVariable
Returns the variable value. This method is meant to be internally called, and will normally perform some kind of initialization for the variable before returning (e.g. database calls, complex computations...)
getValue
in interface ILazyContextVariable<T>
protected abstract T loadValue()
Copyright © 2016 The THYMELEAF team. All rights reserved.