ThymeleafAutoConfiguration: is it possible to provide a way for setting static variables for ThymeleafViewResolver?
I have the same configuration as ThymeleafAutoConfiguration.ThymeleafViewResolverConfiguration with one one little exception: I'm invoking ThymeleafViewResolver.setStaticVariables().
Is it possible to provide a way to do the same thing in Spring Boot auto configuration?
I expect you can just @Autowired a ThymeleafViewResolver and call its setters?
I wanted to do the same thing and that's what I had to do @dsyer. So essentially to answer the question, boot does not provide an easy way to do this.
we could expose that as a property I guess but we should really try to focus on the most common ones. setStaticVariables takes a Map<String,?> and specifying non string values in configuration is tricky so I wouldn't rely on that too much.
@drumonii I've work arounded this issue with the following code:
/**
* Adjusts {@link ThymeleafViewResolver} instance by setting static variables.
*
* @see <a href="https://github.com/spring-projects/spring-boot/issues/3037">spring-boot#3037</a>
**/
@Configuration
public class ThymeleafViewResolverInitializingBean implements InitializingBean, ApplicationContextAware {
private static final Logger LOG = LoggerFactory.getLogger(ThymeleafViewResolverInitializingBean.class);
private ApplicationContext context;
@Override
public void afterPropertiesSet() throws Exception {
ThymeleafViewResolver viewResolver = context.getBean(ThymeleafViewResolver.class);
if (viewResolver == null) {
LOG.warn("Cannot adjust ThymeleafViewResolver: bean of this type wasn't found in application context"
);
return;
}
Map<String,?> staticVars = ...
viewResolver.setStaticVariables(staticVars);
}
@Override
public void setApplicationContext(ApplicationContext context) {
this.context = context;
}
}