我的问题很简单:
在我的 spring-boot web 应用程序中,我有一些前端/客户端需要了解的与 env 相关的属性(比方说,要调用的依赖于 env 的 CORS 远程 url)。
我已经正确定义了我的 application-{ENV}.properties 文件,所有 per-env-props 机制都工作正常。
我似乎找不到答案的问题是:你如何让你的 freemarker 上下文知道你的属性文件以便能够注入(inject)它们(特别是在 spring-boot 应用程序中)。这可能很简单,但我找不到任何例子......
谢谢,
最佳答案
我自己回答:
spring-boot 1.3 中最简单的方法是覆盖 FreeMarkerConfiguration 类:
/**
* Overrides the default spring-boot configuration to allow adding shared variables to the freemarker context
*/
@Configuration
public class FreemarkerConfiguration extends FreeMarkerAutoConfiguration.FreeMarkerWebConfiguration {
@Value("${myProp}")
private String myProp;
@Override
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = super.freeMarkerConfigurer();
Map<String, Object> sharedVariables = new HashMap<>();
sharedVariables.put("myProp", myProp);
configurer.setFreemarkerVariables(sharedVariables);
return configurer;
}
}
关于java - 如何从 freemarker 模板访问 spring boot 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34988679/