【问题标题】:Disable Velocity view resolver in Spring Boot在 Spring Boot 中禁用 Velocity 视图解析器
【发布时间】:2015-07-22 07:27:44
【问题描述】:

我们在应用程序中使用 Spring Boot 以及 AngularJS 和 HTML。我们仅将 Velocity 用于电子邮件模板,而不用于视图解析器。

@Bean(name = "velocityEngine")
public VelocityEngineFactoryBean velocityEngineFactoryBean() {
    VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
    Properties p = new Properties();
    p.put("resource.loader", "class");
    p.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    vefb.setVelocityProperties(p);
    return vefb;
}

即使我们不使用 Velocity 视图解析器,由于自动配置,我们也会收到以下错误:

错误 org.apache.velocity - ResourceManager: 无法找到资源 任何资源加载器中的“LoadList”。错误 org.apache.velocity - ResourceManager:无法在任何资源中找到资源“索引” 装载机。

我尝试禁用 Velocity 自动配置:

@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,VelocityAutoConfiguration.class })
@SuppressWarnings("PMD")
@EnableAsync
public class Application {

还在 application.properties 文件中添加了以下内容:

spring.velocity.check-template-location=false

但我仍然收到上述错误。有没有办法单独禁用 Velocity 视图解析器?

【问题讨论】:

  • 排除应该足以禁用 Velocity 自动配置(它适用于我)。两种可能性:您的应用程序中有另一个 @EnableAutoConfiguration 注释,它没有配置排除;自动配置被禁用,您看到的错误是由于其他原因。也许您可以发布自动配置报告和错误的完整堆栈跟踪?
  • 感谢您的回复。我在其他一些文件中也有@EnableAutoConfiguration。排除其他文件后,它也可以正常工作。
  • 我遇到了类似的问题。我需要设置 spring.velocity.enabled=false 来禁用 Velocity mvc 视图解析器。但是我将 Velocity 用于电子邮件模板,并且设置 spring.velocity.enabled=false 会导致 VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "/mailing.txt", StandardCharsets.UTF_8.name(), model);当项目作为 WAR 文件运行时,不再找到文件“mailing.txt”。在 Eclipse 中运行时它仍然有效。

标签: spring spring-mvc spring-boot velocity


【解决方案1】:

我知道这个问题很老了,但很容易禁用:

添加

spring.velocity.enabled = false

到 application.properties

来源:http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

【讨论】:

    【解决方案2】:

    在 Spring boot 中,使用 Annotation,您可以排除 VelocityAutoConfiguration 之类的

    @SpringBootApplication
    @EnableAutoConfiguration(exclude = {VelocityAutoConfiguration.class})
    @EnableGlobalMethodSecurity(securedEnabled = true)
    public class Application extends SpringBootServletInitializer {
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-04
      • 1970-01-01
      • 2014-08-27
      • 2017-07-08
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      相关资源
      最近更新 更多