spring boot配置freemaker-aut Includes

3437 ワード

最近会社は新しいプロジェクトを始めました.freemarkerを使いました.マクロを使う時はすべてのモデルに自動的にマクロモードを入れたいです.spring bootの自動化配置のソースコードを見に行きました.spring bootの配置の良いspring bootは変わらないと感じて、spring bootのcongigggrationの上で直したいです.
  • まずspring bootのfreemarker Propties
  • を注入します.
        /**
         *   spring boot  fm     
         */
        @Autowired
        private FreeMarkerProperties properties;
  • カバーFree MarkerConfigrer
  •     /**
         *   spring boot  freemarker  
         *
         * @return
         */
        @Bean
        public FreeMarkerConfigurer freeMarkerConfigurer() {
            //    
            FreeMarkerConfigurer factory = new FreeMarkerConfigurer();
            writerProperties(factory);
    
            //  fm   ,   factory       configuration 
            Configuration configuration = null;
            try {
                configuration = factory.createConfiguration();
                // spring boot     ,             freemarker configuration
                List autoIncludes = Lists.newArrayListWithCapacity(1);
                //  macro\macro.ftl       spring.freemarker.template-loader-path 
                autoIncludes.add("macro\\macro.ftl");
                configuration.setAutoIncludes(autoIncludes);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TemplateException e) {
                e.printStackTrace();
            }
            factory.setConfiguration(configuration);
            return factory;
        }
    
        private void writerProperties(FreeMarkerConfigurer factory){
            factory.setTemplateLoaderPaths(this.properties.getTemplateLoaderPath());
            factory.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess());
            factory.setDefaultEncoding(this.properties.getCharsetName());
            Properties settings = new Properties();
            settings.putAll(this.properties.getSettings());
            factory.setFreemarkerSettings(settings);
        }
  • マルコ・ftlは全てのマルコ
  • を含んでいます.
    ここのincludeパスもspring.freemarker.template-loader-pathの下にあります.
    <#include "macro/commonSelect.ftl"/>
    <#include "macro/renderValue.ftl"/>
    <#include "macro/bootSelect.ftl"/>
    <#include "macro/dictToJson.ftl"/>
    
    同理aut Importも同様に書き込み可能です.