001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.ui.freemarker; 018 019import java.io.IOException; 020 021import freemarker.template.Configuration; 022import freemarker.template.TemplateException; 023 024import org.springframework.beans.factory.FactoryBean; 025import org.springframework.beans.factory.InitializingBean; 026import org.springframework.context.ResourceLoaderAware; 027 028/** 029 * Factory bean that creates a FreeMarker Configuration and provides it as 030 * bean reference. This bean is intended for any kind of usage of FreeMarker 031 * in application code, e.g. for generating email content. For web views, 032 * FreeMarkerConfigurer is used to set up a FreeMarkerConfigurationFactory. 033 * 034 * The simplest way to use this class is to specify just a "templateLoaderPath"; 035 * you do not need any further configuration then. For example, in a web 036 * application context: 037 * 038 * <pre class="code"> <bean id="freemarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean"> 039 * <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/> 040 * </bean></pre> 041 042 * See the base class FreeMarkerConfigurationFactory for configuration details. 043 * 044 * <p>Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher. 045 * 046 * @author Darren Davison 047 * @since 03.03.2004 048 * @see #setConfigLocation 049 * @see #setFreemarkerSettings 050 * @see #setTemplateLoaderPath 051 * @see org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer 052 */ 053public class FreeMarkerConfigurationFactoryBean extends FreeMarkerConfigurationFactory 054 implements FactoryBean<Configuration>, InitializingBean, ResourceLoaderAware { 055 056 private Configuration configuration; 057 058 059 @Override 060 public void afterPropertiesSet() throws IOException, TemplateException { 061 this.configuration = createConfiguration(); 062 } 063 064 065 @Override 066 public Configuration getObject() { 067 return this.configuration; 068 } 069 070 @Override 071 public Class<? extends Configuration> getObjectType() { 072 return Configuration.class; 073 } 074 075 @Override 076 public boolean isSingleton() { 077 return true; 078 } 079 080}