Spring boot:使用 Freemarker 找不到模板

use*_*453 2 java spring freemarker spring-batch spring-boot

我有一个 spring boot 应用程序(版本 2.2.5.RELEASE),它打包为 jar 文件:

我正在使用 Freemarker(版本 2.3.29)生成 html 模板,请在下面找到我的配置:

@Configuration
public class EmailFreemarkerConfig {
     @Primary
      @Bean
        public FreeMarkerConfigurationFactoryBean getFreeMarkerConfig() {
            FreeMarkerConfigurationFactoryBean bean = new FreeMarkerConfigurationFactoryBean();
            bean.setTemplateLoaderPath("/templates/");

            return bean;
        }
Run Code Online (Sandbox Code Playgroud)

请找到我的 pom.xml 文件,其中包含 ftl 文件扩展名:

<resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.csv</include>
                    <include>**/*.vm</include>
                    <include>**/*.jpg</include>
                    <include>**/*.ftl</include>
                </includes>
            </resource>
        </resources>
Run Code Online (Sandbox Code Playgroud)

当我打包 jar 时,我可以在以下路径找到该文件:BOOT-INF/classes/templates/file12.ftl

请在下面找到我生成 html 文件的代码:

   @Autowired
    private Configuration freemarkerConfig;

.....
try {
            t = freemarkerConfig.getTemplate("file12.ftl");
            html = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
        } catch (IOException | TemplateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
Run Code Online (Sandbox Code Playgroud)

....当我通过以下命令执行 jar 时:

java -jar test-batch-0.0.2-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)

显示以下异常:

freemarker.template.TemplateNotFoundException: Template not found for name "file12.ftl"
Run Code Online (Sandbox Code Playgroud)

当我在 Eclipse 上的集成测试中执行相同的代码库时,它工作正常。

知道我在这里缺少什么吗?

use*_*453 6

以防万一其他人遇到这个问题,我错过了配置中的类路径:

bean.setTemplateLoaderPath("classpath:/templates/");
Run Code Online (Sandbox Code Playgroud)