Java 如何使用Spring Boot正确配置Velocity?
我是Spring Boot的新手,我在这方面遇到了很大的麻烦。我可以在application.properties中设置一个与速度相关的Spring引导属性的简短列表,这些属性工作正常。但是有大量的速度属性,我不能用这种方式配置 我发现了一个问题,它似乎解决了我的需要,但它对我不起作用。当我在程序启动期间在Spring Boot中使用断点时,我可以看到Spring Boot正确读取和加载了“Spring.velocity.properties.*”键/值对——它们似乎不会影响任何事情。无论我将它们设置为什么值,Velocity运行时行为都使用默认值 我错过了什么 编辑: pom.xml Application.java ExampleVelocityController.java HtmleElement.vmJava 如何使用Spring Boot正确配置Velocity?,java,spring,spring-boot,velocity,Java,Spring,Spring Boot,Velocity,我是Spring Boot的新手,我在这方面遇到了很大的麻烦。我可以在application.properties中设置一个与速度相关的Spring引导属性的简短列表,这些属性工作正常。但是有大量的速度属性,我不能用这种方式配置 我发现了一个问题,它似乎解决了我的需要,但它对我不起作用。当我在程序启动期间在Spring Boot中使用断点时,我可以看到Spring Boot正确读取和加载了“Spring.velocity.properties.*”键/值对——它们似乎不会影响任何事情。无论我将它
#解析(“src/main/resources/templates/general/bodyElement.vm”)
bodyElement.vm
#解析(“src/main/resources/templates/general/bodyElement.vm”)
您应该将断点置于,以确保使用Velocity
看一看。检查依赖项。Velocity肯定正在使用,我可以很好地使用它,我可以看到VelocityAutoConfiguration中使用的直接Spring引导速度属性,如Spring.Velocity.resourceLoaderPath或Spring.Velocity.checkTemplateLocation。我唯一不能做的就是获取spring.Velocity.property。[nativeVelocityProperty]样式属性配置的速度。不幸的是,这个示例没有任何用处,因为它没有尝试做与我相同的事情。详情如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>docuvore</groupId>
<artifactId>docuvore-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Core Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Tomcat and Spring Web MVC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Security
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
-->
<!-- Spring Data and MongoDB -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<!-- Apache Velocity -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>
<!-- Project Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!-- Additional lines to be added here... -->
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
logging.path=/logs
#spring.velocity.resourceLoaderPath = /templates/
#spring.velocity.checkTemplateLocation=false
spring.velocity.properties.template.provide.scope.control = true
spring.velocity.properties.directive.parse.max.depth = 9
spring.velocity.properties.runtime.log = C:/logs/velocity.log
package com.github.docuvore.prototype;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
package com.github.docuvore.prototype.examples;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ExampleVelocityController {
@RequestMapping("/exampleVelocity")
String home() {
String result = null;
VelocityEngine velocity = new VelocityEngine();
velocity.init();
Template template = velocity.getTemplate("src/main/resources/templates/general/htmlElement.vm");
VelocityContext context = new VelocityContext();
context.put("title", "Apache Velocity");
StringWriter writer = new StringWriter();
template.merge(context, writer);
result = writer.toString();
return result;
}
}
<html>
#parse ("src/main/resources/templates/general/bodyElement.vm")
</html>
<body>
#parse ("src/main/resources/templates/general/bodyElement.vm")
</body>