spring-projects / spring-boot

Spring Boot helps you to create Spring-powered, production-grade applications and services with absolute minimum fuss.
https://spring.io/projects/spring-boot
Apache License 2.0
75.02k stars 40.65k forks source link

ThymeleafViewResolver does not pick up cache/cacheLimit from properties #5395

Closed mmoayyed closed 8 years ago

mmoayyed commented 8 years ago

The auto configuration for the ThymeleafViewResolver does:

ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(this.templateEngine);
resolver.setCharacterEncoding(this.properties.getEncoding().name());
resolver.setContentType(appendCharset(this.properties.getContentType(),
                    resolver.getCharacterEncoding()));
resolver.setExcludedViewNames(this.properties.getExcludedViewNames())

The resolver also has setters for cache and cachelimit which are not taken into account from the properties. The resolver as such always ends up caching views, it seems.

snicoll commented 8 years ago

The cache is managed by the TemplateResolver created in the same auto-configuration class.

Did you run in an actual problem? From your description, it's not really clear if you're guessing or you're actually having a problem.

mmoayyed commented 8 years ago

I did run into a problem. I am not sure if what I am doing is purely incorrect and can be done in a better way, but here is what I have as a sample:

  1. Class A extends ThymeleafViewResolver
  2. Class A also has ThymeleafViewResolver autowired in as field, via boot.
  3. When resolving a view, if certain conditions are not met, delegate to the super.
  4. When resolving a view, if certain conditions are met, handle directly.

So, what in order to correctly initialize A as a resolver, I added a PostConstruct method to A to set fields, based on the autowired ThymeleafViewResolver. This allows me to set the template engine and locale and a bunch of other settings on A, that were autowired from the actual ThymeleafViewResolver instance.

This presented the problem/bug, where views seemed always cached by the resolver because ThymeleafViewResolver.isCached() always returned true, and the ThymeleafViewResolver .getCacheLimit() was always > 0.

snicoll commented 8 years ago

What I can see is that you are using a customization of the view resolver and that use cases works fine without the customization. If you want us to look into it, we need a way to make the link between the customization and the issue you're experiencing. Can you share a sample that exhibits the problem?

mmoayyed commented 8 years ago

Happy to remove it, if I can.

Here is the code: https://github.com/Unicon/cas/blob/letsboot/cas-server-webapp-themes/src/main/java/org/jasig/cas/services/web/RegisteredServiceThemeBasedViewResolver.java

mmoayyed commented 8 years ago

Also, perhaps a related yet different issue is boot does not allow me to create something like this: https://github.com/Unicon/cas/blob/letsboot/cas-server-webapp-config/src/main/java/org/jasig/cas/config/CasProtocolViewsConfiguration.java#L212

Which I have had to do in order to compress whitespace that is left by thymeleaf. It would be best actually if boot could do this automatically for me, via a prop, etc.

mmoayyed commented 8 years ago

In the first link, this is the bit that does the job:

        setCache(this.properties.isCache());
        if (!isCache()) {
            setCacheLimit(0);
        }
philwebb commented 8 years ago

I think not setting the cache property on ThymeleafViewResolver is probably a bug, we do it for all other template auto-configurations.

snicoll commented 8 years ago

Indeed, we do it for the ThymeleafResolver but it looks like we forgot to do it for ThymeleafViewResolver.

wilkinsona commented 8 years ago

We don't have a property for cache limit, so I'm only going to apply the spring.thymeleaf.cache property to the auto-configured ThymeleafViewResolver.