I'm trying to have Freemarker's configuration instance initialized and cached by Spring for use in my Restlet app. The issues is that ContextTemplateLoader
takes an argument of type org.restlet.Context
which is accessible via the getContext()
method call in the application. How can one access this context 'inside' the Spring container where Restlet is the 'main container' (i.e. this is a restlet app that uses spring rather than vice versa)?
For now this is how I cache the freemarker configuration:
public class HelloWorldComponent extends Component {
//Cache freemarkerConfiguration
private static Configuration freemarkerConfiguration;
public HelloWorldComponent()
{
freemarkerConfiguration = new Configuration();
// Must pass getContext() as argument - prevents 'springification?'
ContextTemplateLoader loader = new ContextTemplateLoader(getContext(), "war:///WEB-INF");
freemarkerConfiguration.setTemplateLoader(loader);
/* All other beans created and cached by loading the SpringContext */
SpringContext springContext = new SpringContext(getContext());
springContext.getXmlConfigRefs().add("war:///WEB-INF/applicationContext.xml");
getServers().add(Protocol.HTTP);
this.getDefaultHost().attach(new HelloWorldApplication());
}
public static Configuration getFreemarkerConfiguration()
{
return freemarkerConfiguration;
}
I would personally prefer to have the freemarker configuration be cacheable by Spring itself but ContextTemplateLoader
requires a Context
and this isn't accessible outside the application. Having a static method seems like a hack.
What is the easiest/simplest/cleanest way of having spring instantiate freemarker's configuration?
Reason: It's pointless to load the configuration before every access to the template(s) and it's better to cache it. It'd be really neat if this could be defined in Spring itself and the IoC just instantiated in org.restlet.Component
It seems there is a static method Context.getCurrent()
that can be used to fetch the current context but I don't know how to call it through spring (if at all). I am not interested in having Spring MVC libs etc just to create a freemarker configuration instance (there are ways to do that but I don't want Spring related servlets etc.)