@Bean @ConditionalOnMissingBean({FreeMarkerConfig.class, FreeMarkerViewResolver.class}) public FreeMarkerConfigurer freeMarkerConfigurer() { PluginFreeMarkerConfigurer configurer = new PluginFreeMarkerConfigurer(); applyProperties(configurer); String[] paths = this.properties.getTemplateLoaderPath(); // paths = ArrayUtils.add(paths, WEBFTLS_PATH); configurer.setTemplateLoaderPaths(paths); List<WithAnnotationBeanData<FreeMarkerViewTools>> tools = SpringUtils.getBeansWithAnnotation(applicationContext, FreeMarkerViewTools.class); tools.forEach(t->{ String name = t.getAnnotation().value(); if(StringUtils.isBlank(name)){ name = t.getBean().getClass().getSimpleName(); } configurer.setFreemarkerVariable(name, t.getBean()); logger.info("registered FreeMarkerViewTools : {}", name); }); return configurer; }
public static FreeMarkerConfig getFreeMarkerConfig(ApplicationContext applicationContext, String templateLoaderPath) { if (configurer != null) { return configurer; } configurer = getFreeMarkerConfigurer(applicationContext, templateLoaderPath); return configurer; }
public void contextInitialized(ServletContextEvent sc) { logger.info("[5i net registry listener starting...]"); ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(sc.getServletContext()); // 资源实例 ResourceBundle resource = ResourceBundle.getInstance(); resource.setFreemarkerConfig((FreeMarkerConfig) context.getBean("freemarkerConfig")); logger.info("[5i net registry listener finished...]"); }
@Bean @ConditionalOnMissingBean(FreeMarkerConfig.class) public FreeMarkerConfigurer freeMarkerConfigurer() { FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); applyProperties(configurer); return configurer; }
/** * 自动检测FreeMarkerConfig * * @return * @throws BeansException */ protected FreeMarkerConfig autodetectConfiguration() throws BeansException { try { return (FreeMarkerConfig) BeanFactoryUtils .beanOfTypeIncludingAncestors(getApplicationContext(), FreeMarkerConfig.class, true, false); } catch (NoSuchBeanDefinitionException ex) { throw new ApplicationContextException( "Must define a single FreeMarkerConfig bean in this web application context " + "(may be inherited): FreeMarkerConfigurer is the usual implementation. " + "This bean may be given any name.", ex); } }
@Override protected FreeMarkerConfig autodetectConfiguration() throws BeansException { try { return (FreeMarkerConfig) BeanFactoryUtils.beanOfType( getApplicationContext(), FreeMarkerConfig.class, true, false); } catch (NoSuchBeanDefinitionException ex) { throw new ApplicationContextException( "Must define a single FreeMarkerConfig bean in this web application context " + "(may be inherited): FreeMarkerConfigurer is the usual implementation. " + "This bean may be given any name.", ex); } }
@Bean public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException { FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); freeMarkerConfigurer.setConfiguration(new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_22) {{ setTemplateLoader(new MultiTemplateLoader( new TemplateLoader[]{ new ClassTemplateLoader(FreeMarkerConfig.class, "/") } )); setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); setWhitespaceStripping(true); }}); return freeMarkerConfigurer; }
@Override protected FreeMarkerConfig autodetectConfiguration() throws BeansException { System.out.println("autodetectConfiguration:"); String templateLoaderPath = ""; return FreeMarkerUtil.getFreeMarkerConfig(super.getApplicationContext(), templateLoaderPath); }
public FreeMarkerConfig getFreemarkerConfig() { return freemarkerConfig; }
public void setFreemarkerConfig(FreeMarkerConfig freemarkerConfig) { this.freemarkerConfig = freemarkerConfig; }
@Bean public freemarker.template.Configuration freeMarkerConfiguration( FreeMarkerConfig configurer) { return configurer.getConfiguration(); }
/** * Invoked on startup. Looks for a single FreeMarkerConfig bean to find the * relevant Configuration for this factory. * <p> * Checks that the template for the default Locale can be found: FreeMarker * will check non-Locale-specific templates if a locale-specific one is not * found. * * @see freemarker.cache.TemplateCache#getTemplate */ protected void initApplicationContext() throws BeansException { super.initApplicationContext(); if (getConfiguration() == null) { FreeMarkerConfig config = autodetectConfiguration(); setConfiguration(config.getConfiguration()); } checkTemplate(); }