2424import freemarker .template .Template ;
2525import org .junit .Test ;
2626
27+ import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
28+ import org .springframework .beans .factory .support .RootBeanDefinition ;
29+ import org .springframework .beans .factory .xml .XmlBeanDefinitionReader ;
2730import org .springframework .core .io .ByteArrayResource ;
31+ import org .springframework .core .io .ClassPathResource ;
32+ import org .springframework .core .io .DefaultResourceLoader ;
2833import org .springframework .core .io .FileSystemResource ;
2934import org .springframework .core .io .Resource ;
3035import org .springframework .core .io .ResourceLoader ;
4247public class FreeMarkerConfigurerTests {
4348
4449 @ Test (expected = IOException .class )
45- public void freemarkerConfigurationFactoryBeanWithConfigLocation () throws Exception {
50+ public void freeMarkerConfigurationFactoryBeanWithConfigLocation () throws Exception {
4651 FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean ();
4752 fcfb .setConfigLocation (new FileSystemResource ("myprops.properties" ));
4853 Properties props = new Properties ();
@@ -62,22 +67,20 @@ public void freeMarkerConfigurationFactoryBeanWithResourceLoaderPath() throws Ex
6267
6368 @ Test
6469 @ SuppressWarnings ("rawtypes" )
65- public void freemarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath () throws Exception {
70+ public void freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath () throws Exception {
6671 FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean ();
6772 fcfb .setTemplateLoaderPath ("file:/mydir" );
6873 Properties settings = new Properties ();
6974 settings .setProperty ("localized_lookup" , "false" );
7075 fcfb .setFreemarkerSettings (settings );
7176 fcfb .setResourceLoader (new ResourceLoader () {
72-
7377 @ Override
7478 public Resource getResource (String location ) {
7579 if (!("file:/mydir" .equals (location ) || "file:/mydir/test" .equals (location ))) {
7680 throw new IllegalArgumentException (location );
7781 }
7882 return new ByteArrayResource ("test" .getBytes (), "test" );
7983 }
80-
8184 @ Override
8285 public ClassLoader getClassLoader () {
8386 return getClass ().getClassLoader ();
@@ -90,4 +93,16 @@ public ClassLoader getClassLoader() {
9093 assertEquals ("test" , FreeMarkerTemplateUtils .processTemplateIntoString (ft , new HashMap ()));
9194 }
9295
96+ @ Test // SPR-12448
97+ public void freeMarkerConfigurationAsBean () {
98+ DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory ();
99+ RootBeanDefinition loaderDef = new RootBeanDefinition (SpringTemplateLoader .class );
100+ loaderDef .getConstructorArgumentValues ().addGenericArgumentValue (new DefaultResourceLoader ());
101+ loaderDef .getConstructorArgumentValues ().addGenericArgumentValue ("/freemarker" );
102+ RootBeanDefinition configDef = new RootBeanDefinition (Configuration .class );
103+ configDef .getPropertyValues ().add ("templateLoader" , loaderDef );
104+ beanFactory .registerBeanDefinition ("freeMarkerConfig" , configDef );
105+ beanFactory .getBean (Configuration .class );
106+ }
107+
93108}
0 commit comments