spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

ThymeleafAutoConfiguration: is it possible to provide a way for setting static variables for ThymeleafViewResolver?

Open php-coder opened this issue 11 years ago • 4 comments

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?

php-coder avatar May 25 '15 19:05 php-coder

I expect you can just @Autowired a ThymeleafViewResolver and call its setters?

dsyer avatar May 26 '15 06:05 dsyer

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.

drumonii avatar May 26 '15 13:05 drumonii

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.

snicoll avatar May 26 '15 13:05 snicoll

@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;
    }

}

php-coder avatar May 26 '15 14:05 php-coder