|
From: Darren D. <da...@da...> - 2004-03-09 15:49:11
|
> Yeah.. The shared varible system works. Wrapping a map in a simple has=
h
> is
> the same technique that I have used elsewhere.
good :)
> This will make writing an email template system quite easy :
>
> <bean id=3D"feedbackTransformer"
> class=3D"spring.ui.freemarker.TransformerFactoryBean" singleton=3D"fals=
e">
> <property
> name=3D"templateLocation"><value>classpath:com/acme/feedback.ftl</value=
></prop>
erty>
> <!-- all properties of the context are entered into a simple hash -->
> <property name=3D"context">
> <map>etc...</map>
> </property>
> </bean>
>
> Then in your code you can do
>
> TransformerFactoryBean transformer
> (TransformerFactoryBean)context.getBean("feedbackTransformer");
>
> transformer.addParam("name", model.getName());
>
> emailSender.send("fee...@ac...", "feedback",
> transformer.transform());
The problem is that you can't set shared variables on a template unless
you retrieve its configuration to do so - which seems to me to have
reduced value in that you either still require the
ConfigurationFactoryBean in order to share it amongst the template beans
you define, or you end up with separate instances of Configuration object=
s
per template.
Note that you can achieve what you want (I think) from the existing
ConfigurationFactoryBean. Set the freemarkerSettings and
freemarkerVariables as required and then just get the template from the
config via a MethodInvokingFactoryBean. Any other 'context properties'
(are you talking about beans in the application context?) can be set by
Spring on your domain object in the normal manner.
(all this is off the top of my head - not tried any of it)
<bean id=3D"fmConfiguration"
class=3D"spring.ui.freemarker.ConfigurationFactoryBean">
<property name=3D"freemarkerVariables">
<map>etc...</map>
</property>
</bean>
<bean id=3D"fmTemplate"
class=3D"[spring].beans.factory.config.MethodInvokingFactoryBean">
<property name=3D"targetObject"><ref local=3D"fmConfiguration"/></prope=
rty>
<property name=3D"targetMethod"><value>getTemplate</value></property>
<property name=3D"args">
<list>
<value>path/to/my/template.ftl</value>
</list>
</property>
</bean>
<bean id=3D"myObject"
class=3D"com.myco.MyDomainClass">
<property name=3D"template"><ref local=3D"fmTemplate"/></property>
</bean>
- and -
public class MyDomainClass {
private Template template;
/**
* have Spring set the template as a bean property
*/
public void setTemplate(Template template) {
this.template =3D template;
}
public void myLogic() {
/* create modelMap */
...
template.process(modelMap, someWriter);
}
}
I can see value in reducing the configuration by defining a
TemplateFactory though, so let me know what you think?
Regards,
--=20
Darren Davison
Public Key: http://www.davison.uk.net/key.jsp
|