2121
2222import org .springframework .boot .bind .PropertySourcesPropertyValues ;
2323import org .springframework .boot .bind .RelaxedDataBinder ;
24+ import org .springframework .boot .bind .RelaxedPropertyResolver ;
2425import org .springframework .context .EnvironmentAware ;
2526import org .springframework .core .env .ConfigurableEnvironment ;
2627import org .springframework .core .env .Environment ;
2728
2829import com .samskivert .mustache .DefaultCollector ;
29- import com .samskivert .mustache .Mustache ;
3030import com .samskivert .mustache .Mustache .Collector ;
3131import com .samskivert .mustache .Mustache .VariableFetcher ;
3232
@@ -43,42 +43,38 @@ public class MustacheEnvironmentCollector extends DefaultCollector implements
4343
4444 private Map <String , Object > target ;
4545
46+ private RelaxedPropertyResolver propertyResolver ;
47+
48+ private final VariableFetcher propertyFetcher = new PropertyVariableFetcher ();
49+
4650 @ Override
4751 public void setEnvironment (Environment environment ) {
4852 this .environment = (ConfigurableEnvironment ) environment ;
4953 this .target = new HashMap <String , Object >();
5054 new RelaxedDataBinder (this .target ).bind (new PropertySourcesPropertyValues (
5155 this .environment .getPropertySources ()));
56+ this .propertyResolver = new RelaxedPropertyResolver (environment );
5257 }
5358
5459 @ Override
55- public Mustache . VariableFetcher createFetcher (Object ctx , String name ) {
60+ public VariableFetcher createFetcher (Object ctx , String name ) {
5661 VariableFetcher fetcher = super .createFetcher (ctx , name );
5762 if (fetcher != null ) {
5863 return fetcher ;
5964 }
60- if (this .environment .containsProperty (name )) {
61- return new VariableFetcher () {
62-
63- @ Override
64- public Object get (Object ctx , String name ) throws Exception {
65- return MustacheEnvironmentCollector .this .environment
66- .getProperty (name );
67- }
68-
69- };
65+ if (this .propertyResolver .containsProperty (name )) {
66+ return this .propertyFetcher ;
7067 }
71- if ( this . target . containsKey ( name )) {
72- return new VariableFetcher () {
68+ return null ;
69+ }
7370
74- @ Override
75- public Object get (Object ctx , String name ) throws Exception {
76- return MustacheEnvironmentCollector .this .target .get (name );
77- }
71+ private class PropertyVariableFetcher implements VariableFetcher {
7872
79- };
73+ @ Override
74+ public Object get (Object ctx , String name ) throws Exception {
75+ return MustacheEnvironmentCollector .this .propertyResolver .getProperty (name );
8076 }
81- return null ;
77+
8278 }
8379
8480}
0 commit comments