-
Notifications
You must be signed in to change notification settings - Fork 40.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a callback for customising Freemarker variables #8965
Comments
What do you mean by "freemarkerVariables"? Can you please show what you're currently having to configure that the auto-configuration should be able to configure for you? |
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
but FreeMarkerConfigurationFactory has freemarkerVariables,i need it |
This comment was marked as duplicate.
This comment was marked as duplicate.
Ok, so you'd like to be able to set the What variables do you want to be able to set? Can you please provide some specific examples? Please note that values other than maps, lists and scalar values are not well-suited to being set via |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as resolved.
This comment was marked as resolved.
and configuration is
|
in FreeMarkerAutoConfiguration ,and join factory.setFreemarkerVariables(this.properties.getFreemarkerVariables());
|
This comment was marked as outdated.
This comment was marked as outdated.
The formatting is much better, thank you. Unfortunately, it's not really what it was looking for. You've shown how what you're asking for could be implemented in Spring Boot and I already know how to do that. What I don't know is what values you want to set in If you want to configure something that's more complex, or you just want to do this without an enhancement to Spring Boot, then registering a |
workd for me |
Spring Boot 2.0 breaks the solution provided by @wo8335224, as A currently working solution is to configure
Internally |
Hey @bianjp ! Yes, you can do this, but you'll lose some auto-configuration. @Component
class FreeMarkerPostProcessor : BeanPostProcessor {
override fun postProcessBeforeInitialization(bean: Any, beanName: String): Any {
if (bean is FreeMarkerConfigurer) {
bean.setFreemarkerVariables(mapOf(
"globalVar" to "globalVarValue"
))
}
return bean
}
} |
Thanks @satahippy. Java version: @Configuration
public class CustomFreeMarkerConfig implements BeanPostProcessor {
Object sharedWithAllFreeMarkerTemplatesObj = new Object();
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof FreeMarkerConfigurer) {
FreeMarkerConfigurer configurer = (FreeMarkerConfigurer) bean;
Map<String, Object> sharedVariables = new HashMap<>();
sharedVariables.put("globalVar", sharedWithAllFreeMarkerTemplatesObj);
configurer.setFreemarkerVariables(sharedVariables);
}
return bean;
}
} [edit: my original comment confused Before/After Initialization) I feel like there should be some easier, built-in way, of doing this, but I haven't found it yet. Related question on StackOverflow: https://stackoverflow.com/questions/52730368/how-to-make-globally-shared-objects-available-to-freemarker-templates-in-spring |
This comment was marked as outdated.
This comment was marked as outdated.
+1 for this. Today I encountered the same need. |
I don't think this needs to be blocked on the Framework issue. While allowing the variables to be amended could be useful, being able to set them (overwriting anything already set) would still be a step forwards. As and when there's a Framework API that allows the variables to be amended, that would be an additional benefit of a customizer callback in Boot. |
boot automatic configuration freemarker lack freemarkerVariables
The text was updated successfully, but these errors were encountered: