Merge branch '1.5.x'

This commit is contained in:
Phillip Webb
2017-04-11 21:07:51 -07:00
12 changed files with 208 additions and 156 deletions

View File

@@ -20,13 +20,8 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.boot.autoconfigure.template.AbstractTemplateAvailabilityProvider;
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider;
import org.springframework.boot.bind.PropertySourcesPropertyValues;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ClassUtils;
/**
* {@link TemplateAvailabilityProvider} that provides availability information for
@@ -36,35 +31,28 @@ import org.springframework.util.ClassUtils;
* @since 1.1.0
*/
public class FreeMarkerTemplateAvailabilityProvider
implements TemplateAvailabilityProvider {
extends AbstractTemplateAvailabilityProvider {
@Override
public boolean isTemplateAvailable(String view, Environment environment,
ClassLoader classLoader, ResourceLoader resourceLoader) {
if (ClassUtils.isPresent("freemarker.template.Configuration", classLoader)) {
FreeMarkerTemplateAvailabilityProperties properties = new FreeMarkerTemplateAvailabilityProperties();
RelaxedDataBinder binder = new RelaxedDataBinder(properties,
"spring.freemarker");
binder.bind(new PropertySourcesPropertyValues(
((ConfigurableEnvironment) environment).getPropertySources()));
for (String loaderPath : properties.getTemplateLoaderPath()) {
if (resourceLoader.getResource(loaderPath + properties.getPrefix() + view
+ properties.getSuffix()).exists()) {
return true;
}
}
}
return false;
public FreeMarkerTemplateAvailabilityProvider() {
super("freemarker.template.Configuration",
FreeMarkerTemplateAvailabilityProperties.class, "spring.freemarker");
}
static final class FreeMarkerTemplateAvailabilityProperties {
static final class FreeMarkerTemplateAvailabilityProperties
extends TemplateAvailabilityProperties {
private List<String> templateLoaderPath = new ArrayList<String>(
Arrays.asList(FreeMarkerProperties.DEFAULT_TEMPLATE_LOADER_PATH));
private String prefix = FreeMarkerProperties.DEFAULT_PREFIX;
FreeMarkerTemplateAvailabilityProperties() {
super(FreeMarkerProperties.DEFAULT_PREFIX,
FreeMarkerProperties.DEFAULT_SUFFIX);
}
private String suffix = FreeMarkerProperties.DEFAULT_SUFFIX;
@Override
protected List<String> getLoaderPath() {
return this.templateLoaderPath;
}
public List<String> getTemplateLoaderPath() {
return this.templateLoaderPath;
@@ -74,22 +62,6 @@ public class FreeMarkerTemplateAvailabilityProvider
this.templateLoaderPath = templateLoaderPath;
}
public String getPrefix() {
return this.prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return this.suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
}

View File

@@ -20,13 +20,8 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.boot.autoconfigure.template.AbstractTemplateAvailabilityProvider;
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider;
import org.springframework.boot.bind.PropertySourcesPropertyValues;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ClassUtils;
/**
* {@link TemplateAvailabilityProvider} that provides availability information for Groovy
@@ -35,35 +30,29 @@ import org.springframework.util.ClassUtils;
* @author Dave Syer
* @since 1.1.0
*/
public class GroovyTemplateAvailabilityProvider implements TemplateAvailabilityProvider {
public class GroovyTemplateAvailabilityProvider
extends AbstractTemplateAvailabilityProvider {
@Override
public boolean isTemplateAvailable(String view, Environment environment,
ClassLoader classLoader, ResourceLoader resourceLoader) {
if (ClassUtils.isPresent("groovy.text.TemplateEngine", classLoader)) {
GroovyTemplateAvailabilityProperties properties = new GroovyTemplateAvailabilityProperties();
RelaxedDataBinder binder = new RelaxedDataBinder(properties,
"spring.groovy.template");
binder.bind(new PropertySourcesPropertyValues(
((ConfigurableEnvironment) environment).getPropertySources()));
for (String loaderPath : properties.getResourceLoaderPath()) {
if (resourceLoader.getResource(loaderPath + properties.getPrefix() + view
+ properties.getSuffix()).exists()) {
return true;
}
}
}
return false;
public GroovyTemplateAvailabilityProvider() {
super("groovy.text.TemplateEngine", GroovyTemplateAvailabilityProperties.class,
"spring.groovy.template");
}
static final class GroovyTemplateAvailabilityProperties {
static final class GroovyTemplateAvailabilityProperties
extends TemplateAvailabilityProperties {
private List<String> resourceLoaderPath = new ArrayList<String>(
Arrays.asList(GroovyTemplateProperties.DEFAULT_RESOURCE_LOADER_PATH));
private String prefix = GroovyTemplateProperties.DEFAULT_PREFIX;
GroovyTemplateAvailabilityProperties() {
super(GroovyTemplateProperties.DEFAULT_PREFIX,
GroovyTemplateProperties.DEFAULT_SUFFIX);
}
private String suffix = GroovyTemplateProperties.DEFAULT_SUFFIX;
@Override
protected List<String> getLoaderPath() {
return this.resourceLoaderPath;
}
public List<String> getResourceLoaderPath() {
return this.resourceLoaderPath;
@@ -73,22 +62,6 @@ public class GroovyTemplateAvailabilityProvider implements TemplateAvailabilityP
this.resourceLoaderPath = resourceLoaderPath;
}
public String getPrefix() {
return this.prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return this.suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
}

View File

@@ -0,0 +1,110 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.template;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.bind.PropertySourcesPropertyValues;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ClassUtils;
/**
* Abstract base class for {@link TemplateAvailabilityProvider} implementations.
*
* @author Andy Wilkinson
* @author Phillip Webb
* @since 1.4.6
*/
public abstract class AbstractTemplateAvailabilityProvider
implements TemplateAvailabilityProvider {
private final String className;
private final Class<? extends TemplateAvailabilityProperties> propertiesClass;
private final String propertyPrefix;
public AbstractTemplateAvailabilityProvider(String className,
Class<? extends TemplateAvailabilityProperties> propertiesClass,
String propertyPrefix) {
this.className = className;
this.propertiesClass = propertiesClass;
this.propertyPrefix = propertyPrefix;
}
@Override
public boolean isTemplateAvailable(String view, Environment environment,
ClassLoader classLoader, ResourceLoader resourceLoader) {
if (ClassUtils.isPresent(this.className, classLoader)) {
TemplateAvailabilityProperties properties = BeanUtils
.instantiateClass(this.propertiesClass);
RelaxedDataBinder binder = new RelaxedDataBinder(properties,
this.propertyPrefix);
binder.bind(new PropertySourcesPropertyValues(
((ConfigurableEnvironment) environment).getPropertySources()));
return isTemplateAvailable(view, resourceLoader, properties);
}
return false;
}
private boolean isTemplateAvailable(String view, ResourceLoader resourceLoader,
TemplateAvailabilityProperties properties) {
String location = properties.getPrefix() + view + properties.getSuffix();
for (String path : properties.getLoaderPath()) {
if (resourceLoader.getResource(path + location).exists()) {
return true;
}
}
return false;
}
protected static abstract class TemplateAvailabilityProperties {
private String prefix;
private String suffix;
protected TemplateAvailabilityProperties(String prefix, String suffix) {
this.prefix = prefix;
this.suffix = suffix;
}
protected abstract List<String> getLoaderPath();
public String getPrefix() {
return this.prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return this.suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
}

View File

@@ -66,8 +66,8 @@ public class ValidationAutoConfiguration {
}
private static boolean determineProxyTargetClass(Environment environment) {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
environment, "spring.aop.");
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment,
"spring.aop.");
Boolean value = resolver.getProperty("proxyTargetClass", Boolean.class);
return (value != null ? value : true);
}

View File

@@ -78,12 +78,12 @@ public class ValidationAutoConfigurationTests {
@Test
public void validationCanBeConfiguredToUseJdkProxy() {
load(AnotherSampleServiceConfiguration.class, "spring.aop.proxy-target-class=false");
load(AnotherSampleServiceConfiguration.class,
"spring.aop.proxy-target-class=false");
assertThat(this.context.getBeansOfType(Validator.class)).hasSize(1);
assertThat(this.context.getBeansOfType(DefaultAnotherSampleService.class))
.isEmpty();
AnotherSampleService service = this.context
.getBean(AnotherSampleService.class);
.isEmpty();
AnotherSampleService service = this.context.getBean(AnotherSampleService.class);
service.doSomething(42);
this.thrown.expect(ConstraintViolationException.class);
service.doSomething(2);