diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheEnvironmentCollector.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheEnvironmentCollector.java index 62be1e0b509..e2dadb570c3 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheEnvironmentCollector.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheEnvironmentCollector.java @@ -21,12 +21,12 @@ import java.util.Map; import org.springframework.boot.bind.PropertySourcesPropertyValues; import org.springframework.boot.bind.RelaxedDataBinder; +import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.context.EnvironmentAware; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import com.samskivert.mustache.DefaultCollector; -import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Mustache.Collector; import com.samskivert.mustache.Mustache.VariableFetcher; @@ -43,42 +43,38 @@ public class MustacheEnvironmentCollector extends DefaultCollector implements private Map target; + private RelaxedPropertyResolver propertyResolver; + + private final VariableFetcher propertyFetcher = new PropertyVariableFetcher(); + @Override public void setEnvironment(Environment environment) { this.environment = (ConfigurableEnvironment) environment; this.target = new HashMap(); new RelaxedDataBinder(this.target).bind(new PropertySourcesPropertyValues( this.environment.getPropertySources())); + this.propertyResolver = new RelaxedPropertyResolver(environment); } @Override - public Mustache.VariableFetcher createFetcher(Object ctx, String name) { + public VariableFetcher createFetcher(Object ctx, String name) { VariableFetcher fetcher = super.createFetcher(ctx, name); if (fetcher != null) { return fetcher; } - if (this.environment.containsProperty(name)) { - return new VariableFetcher() { - - @Override - public Object get(Object ctx, String name) throws Exception { - return MustacheEnvironmentCollector.this.environment - .getProperty(name); - } - - }; - } - if (this.target.containsKey(name)) { - return new VariableFetcher() { - - @Override - public Object get(Object ctx, String name) throws Exception { - return MustacheEnvironmentCollector.this.target.get(name); - } - - }; + if (this.propertyResolver.containsProperty(name)) { + return this.propertyFetcher; } return null; } + private class PropertyVariableFetcher implements VariableFetcher { + + @Override + public Object get(Object ctx, String name) throws Exception { + return MustacheEnvironmentCollector.this.propertyResolver.getProperty(name); + } + + } + }