mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Simplify MustacheEnvironmentCollector
Simplify the MustacheEnvironmentCollector so that it no longer binds the Spring Environment to a Map. See gh-2242
This commit is contained in:
parent
d5a82aaed8
commit
c1dcd85f60
@ -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<String, Object> target;
|
||||
|
||||
private RelaxedPropertyResolver propertyResolver;
|
||||
|
||||
private final VariableFetcher propertyFetcher = new PropertyVariableFetcher();
|
||||
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
this.environment = (ConfigurableEnvironment) environment;
|
||||
this.target = new HashMap<String, Object>();
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user