Skip to content

Commit 8a5f151

Browse files
author
Dave Syer
committed
Add spring.*.enabled for MVC view resolvers
Since we use a composite ViewResolver glbally by default it can be awkward to switch off the view technology that you have on the classpath but aren't actually using.
1 parent d0d640b commit 8a5f151

File tree

9 files changed

+70
-0
lines changed

9 files changed

+70
-0
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3030
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3131
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
32+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3233
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
3334
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
3435
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -134,6 +135,7 @@ public freemarker.template.Configuration freeMarkerConfiguration(
134135

135136
@Bean
136137
@ConditionalOnMissingBean(name = "freeMarkerViewResolver")
138+
@ConditionalOnProperty(name = "spring.freemarker.enabled", matchIfMissing = true)
137139
public FreeMarkerViewResolver freeMarkerViewResolver() {
138140
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
139141
this.properties.applyToViewResolver(resolver);

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,23 @@ public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties
4848
*/
4949
private String[] templateLoaderPath = new String[] { DEFAULT_TEMPLATE_LOADER_PATH };
5050

51+
/**
52+
* Switches off MVC view resolution if set to false (default true).
53+
*/
54+
private boolean enabled = true;
55+
5156
public FreeMarkerProperties() {
5257
super(DEFAULT_PREFIX, DEFAULT_SUFFIX);
5358
}
5459

60+
public boolean isEnabled() {
61+
return this.enabled;
62+
}
63+
64+
public void setEnabled(boolean enabled) {
65+
this.enabled = enabled;
66+
}
67+
5568
public Map<String, String> getSettings() {
5669
return this.settings;
5770
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2727
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
28+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2829
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2930
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
3031
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -103,6 +104,7 @@ public GroovyMarkupConfigurer groovyMarkupConfigurer() {
103104
@ConditionalOnClass({ Servlet.class, LocaleContextHolder.class,
104105
UrlBasedViewResolver.class })
105106
@ConditionalOnWebApplication
107+
@ConditionalOnProperty(name = "spring.groovy.template.enabled", matchIfMissing = true)
106108
public static class GroovyWebConfiguration {
107109

108110
@Autowired

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ public class GroovyTemplateProperties extends AbstractViewResolverProperties {
5050
*/
5151
private Map<String, Object> configuration = new HashMap<String, Object>();
5252

53+
/**
54+
* Switches off MVC view resolution if set to false (default true).
55+
*/
56+
private boolean enabled = true;
57+
58+
public boolean isEnabled() {
59+
return this.enabled;
60+
}
61+
62+
public void setEnabled(boolean enabled) {
63+
this.enabled = enabled;
64+
}
65+
5366
public String getPrefix() {
5467
return this.prefix;
5568
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3030
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3131
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
32+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3233
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
3334
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
3435
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -175,6 +176,7 @@ protected static class ThymeleafViewResolverConfiguration {
175176

176177
@Bean
177178
@ConditionalOnMissingBean(name = "thymeleafViewResolver")
179+
@ConditionalOnProperty(name = "spring.thymeleaf.enabled", matchIfMissing = true)
178180
public ThymeleafViewResolver thymeleafViewResolver() {
179181
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
180182
resolver.setTemplateEngine(this.templateEngine);

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ public class ThymeleafProperties {
7676
*/
7777
private String[] excludedViewNames;
7878

79+
/**
80+
* Switches off MVC view resolution if set to false (default true).
81+
*/
82+
private boolean enabled = true;
83+
84+
public boolean isEnabled() {
85+
return this.enabled;
86+
}
87+
88+
public void setEnabled(boolean enabled) {
89+
this.enabled = enabled;
90+
}
91+
7992
public boolean isCheckTemplateLocation() {
8093
return this.checkTemplateLocation;
8194
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3131
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3232
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
33+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3334
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
3435
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
3536
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -124,6 +125,7 @@ public VelocityEngine velocityEngine(VelocityConfigurer configurer)
124125

125126
@Bean
126127
@ConditionalOnMissingBean(name = "velocityViewResolver")
128+
@ConditionalOnProperty(name = "spring.velocity.enabled", matchIfMissing = true)
127129
public VelocityViewResolver velocityViewResolver() {
128130
VelocityViewResolver resolver = new VelocityViewResolver();
129131
this.properties.applyToViewResolver(resolver);

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,23 @@ public class VelocityProperties extends AbstractTemplateViewResolverProperties {
7171
*/
7272
private boolean preferFileSystemAccess = true;
7373

74+
/**
75+
* Switches off MVC view resolution if set to false (default true).
76+
*/
77+
private boolean enabled = true;
78+
7479
public VelocityProperties() {
7580
super(DEFAULT_PREFIX, DEFAULT_SUFFIX);
7681
}
7782

83+
public boolean isEnabled() {
84+
return this.enabled;
85+
}
86+
87+
public void setEnabled(boolean enabled) {
88+
this.enabled = enabled;
89+
}
90+
7891
public String getDateToolAttribute() {
7992
return this.dateToolAttribute;
8093
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfigurationTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.mock.web.MockServletContext;
3939
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
4040
import org.springframework.web.servlet.View;
41+
import org.springframework.web.servlet.ViewResolver;
4142
import org.springframework.web.servlet.support.RequestContext;
4243
import org.springframework.web.servlet.view.groovy.GroovyMarkupConfig;
4344
import org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver;
@@ -100,6 +101,15 @@ public void includesViewResolution() throws Exception {
100101
assertThat(response.getContentType(), equalTo("text/html;charset=UTF-8"));
101102
}
102103

104+
@Test
105+
public void disableViewResolution() throws Exception {
106+
EnvironmentTestUtils.addEnvironment(this.context,
107+
"spring.groovy.template.enabled:false");
108+
registerAndRefreshContext();
109+
assertThat(this.context.getBeanNamesForType(ViewResolver.class).length,
110+
equalTo(0));
111+
}
112+
103113
@Test
104114
public void localeViewResolution() throws Exception {
105115
registerAndRefreshContext();

0 commit comments

Comments
 (0)