From 7f036e35fdd5d6a5e575dbfea54cd179aa390930 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 16 Mar 2022 19:07:38 +0000 Subject: [PATCH] Make MustacheViewResolvers conditional onspring.mustache.enabled Closes gh-30250 --- .../MustacheReactiveWebConfiguration.java | 4 +++- .../MustacheServletWebConfiguration.java | 4 +++- .../MustacheAutoConfigurationTests.java | 21 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheReactiveWebConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheReactiveWebConfiguration.java index 4aa926fafa..729fb82b55 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheReactiveWebConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheReactiveWebConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.mustache; import com.samskivert.mustache.Mustache.Compiler; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; import org.springframework.boot.web.reactive.result.view.MustacheViewResolver; @@ -32,6 +33,7 @@ class MustacheReactiveWebConfiguration { @Bean @ConditionalOnMissingBean + @ConditionalOnProperty(prefix = "spring.mustache", name = "enabled", matchIfMissing = true) MustacheViewResolver mustacheViewResolver(Compiler mustacheCompiler, MustacheProperties mustache) { MustacheViewResolver resolver = new MustacheViewResolver(mustacheCompiler); resolver.setPrefix(mustache.getPrefix()); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheServletWebConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheServletWebConfiguration.java index 884a019ecd..0ff0462802 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheServletWebConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheServletWebConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.mustache; import com.samskivert.mustache.Mustache.Compiler; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; import org.springframework.boot.web.servlet.view.MustacheViewResolver; @@ -32,6 +33,7 @@ class MustacheServletWebConfiguration { @Bean @ConditionalOnMissingBean + @ConditionalOnProperty(prefix = "spring.mustache", name = "enabled", matchIfMissing = true) MustacheViewResolver mustacheViewResolver(Compiler mustacheCompiler, MustacheProperties mustache) { MustacheViewResolver resolver = new MustacheViewResolver(mustacheCompiler); mustache.applyToMvcViewResolver(resolver); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java index 2960eb96bf..a3669c0930 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java @@ -46,6 +46,16 @@ class MustacheAutoConfigurationTests { }); } + @Test + void servletViewResolverCanBeDisabled() { + configure(new WebApplicationContextRunner()).withPropertyValues("spring.mustache.enabled=false") + .run((context) -> { + assertThat(context).hasSingleBean(Mustache.Compiler.class); + assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class); + assertThat(context).doesNotHaveBean(MustacheViewResolver.class); + }); + } + @Test void registerCompilerForServletApp() { configure(new WebApplicationContextRunner()).withUserConfiguration(CustomCompilerConfiguration.class) @@ -68,6 +78,17 @@ class MustacheAutoConfigurationTests { }); } + @Test + void reactiveViewResolverCanBeDisabled() { + configure(new ReactiveWebApplicationContextRunner()).withPropertyValues("spring.mustache.enabled=false") + .run((context) -> { + assertThat(context).hasSingleBean(Mustache.Compiler.class); + assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class); + assertThat(context).doesNotHaveBean( + org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class); + }); + } + @Test void registerCompilerForReactiveApp() { configure(new ReactiveWebApplicationContextRunner()).withUserConfiguration(CustomCompilerConfiguration.class)