001/* 002 * Copyright 2002-2017 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; 027import org.springframework.lang.Nullable; 028 029/** 030 * Factory bean that creates a FreeMarker Configuration and provides it as 031 * bean reference. This bean is intended for any kind of usage of FreeMarker 032 * in application code, e.g. for generating email content. For web views, 033 * FreeMarkerConfigurer is used to set up a FreeMarkerConfigurationFactory. 034 * 035 * The simplest way to use this class is to specify just a "templateLoaderPath"; 036 * you do not need any further configuration then. For example, in a web 037 * application context: 038 * 039 * <pre class="code"> <bean id="freemarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean"> 040 * <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/> 041 * </bean></pre> 042 043 * See the base class FreeMarkerConfigurationFactory for configuration details. 044 * 045 * <p>Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher. 046 * 047 * @author Darren Davison 048 * @since 03.03.2004 049 * @see #setConfigLocation 050 * @see #setFreemarkerSettings 051 * @see #setTemplateLoaderPath 052 * @see org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer 053 */ 054public class FreeMarkerConfigurationFactoryBean extends FreeMarkerConfigurationFactory 055 implements FactoryBean<Configuration>, InitializingBean, ResourceLoaderAware { 056 057 @Nullable 058 private Configuration configuration; 059 060 061 @Override 062 public void afterPropertiesSet() throws IOException, TemplateException { 063 this.configuration = createConfiguration(); 064 } 065 066 067 @Override 068 @Nullable 069 public Configuration getObject() { 070 return this.configuration; 071 } 072 073 @Override 074 public Class<? extends Configuration> getObjectType() { 075 return Configuration.class; 076 } 077 078 @Override 079 public boolean isSingleton() { 080 return true; 081 } 082 083}