Java Velocity 2.0无法在jar中找到模板资源
我在Spring Boot 5中使用Velocity 2.0发送电子邮件。我从src/main/resources/email/加载模板,并在config.xml文件中将VelocityEngine定义为beanJava Velocity 2.0无法在jar中找到模板资源,java,spring-boot,velocity,Java,Spring Boot,Velocity,我在Spring Boot 5中使用Velocity 2.0发送电子邮件。我从src/main/resources/email/加载模板,并在config.xml文件中将VelocityEngine定义为bean <bean id="velocityEngine" class="org.apache.velocity.app.VelocityEngine"> <property name="properties"> <props>
<bean id="velocityEngine" class="org.apache.velocity.app.VelocityEngine">
<property name="properties">
<props>
<prop key="resource.loader">file</prop>
<prop key="file.resource.loader.class">org.apache.velocity.runtime.resource.loader.FileResourceLoader</prop>
<prop key="file.resource.loader.path">src/main/resources/templates/email/</prop>
<prop key="file.resource.loader.cache">true</prop>
<prop key="file.resource.loader.modificationCheckInterval">5</prop>
</props>
</property>
</bean>
文件
org.apache.velocity.runtime.resource.loader.FileResourceLoader
src/main/resources/templates/email/
真的
5.
当我从Intellij IDEA运行它时,一切都很好,但当我构建jar在服务器上运行它时,它抛出异常:
org.apache.velocity.exception.ResourceNotFoundException:找不到资源“MyTemplate.vm”。知道怎么修吗?如果您能提供任何帮助,我将不胜感激。因此,您正在使用FileResourceLoader并为其提供相对路径“src/main/resources/templates/email/” 因此,除非与应用程序运行位置相关的路径在服务器上,否则它会给出一个完全合理的错误代码
您想在bean配置中使用,并确保模板在生成的jar中正确打包。我已将FileResourceLoader更改为ClasspathResourceLoader,将模板移动到src/main/resources,并将file.resource.loader.path定义为:classpath。之后,它开始使用一个罐子。但是通过阅读文档,我仍然需要将模板保存在单独的目录/email中(我会有很多模板),而不是src/main/resources中。文档注意到ClassPathResource加载器是一个无配置的加载器,资源只需要位于类路径上。file.resource.loader.path不再影响任何内容。将模板放在“src/main/resources/templates/email/”中,然后从“/templates/email/my email template.vtl”加载它们