21
21
22
22
import org .springframework .boot .bind .PropertySourcesPropertyValues ;
23
23
import org .springframework .boot .bind .RelaxedDataBinder ;
24
+ import org .springframework .boot .bind .RelaxedPropertyResolver ;
24
25
import org .springframework .context .EnvironmentAware ;
25
26
import org .springframework .core .env .ConfigurableEnvironment ;
26
27
import org .springframework .core .env .Environment ;
27
28
28
29
import com .samskivert .mustache .DefaultCollector ;
29
- import com .samskivert .mustache .Mustache ;
30
30
import com .samskivert .mustache .Mustache .Collector ;
31
31
import com .samskivert .mustache .Mustache .VariableFetcher ;
32
32
@@ -43,42 +43,38 @@ public class MustacheEnvironmentCollector extends DefaultCollector implements
43
43
44
44
private Map <String , Object > target ;
45
45
46
+ private RelaxedPropertyResolver propertyResolver ;
47
+
48
+ private final VariableFetcher propertyFetcher = new PropertyVariableFetcher ();
49
+
46
50
@ Override
47
51
public void setEnvironment (Environment environment ) {
48
52
this .environment = (ConfigurableEnvironment ) environment ;
49
53
this .target = new HashMap <String , Object >();
50
54
new RelaxedDataBinder (this .target ).bind (new PropertySourcesPropertyValues (
51
55
this .environment .getPropertySources ()));
56
+ this .propertyResolver = new RelaxedPropertyResolver (environment );
52
57
}
53
58
54
59
@ Override
55
- public Mustache . VariableFetcher createFetcher (Object ctx , String name ) {
60
+ public VariableFetcher createFetcher (Object ctx , String name ) {
56
61
VariableFetcher fetcher = super .createFetcher (ctx , name );
57
62
if (fetcher != null ) {
58
63
return fetcher ;
59
64
}
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 ;
70
67
}
71
- if ( this . target . containsKey ( name )) {
72
- return new VariableFetcher () {
68
+ return null ;
69
+ }
73
70
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 {
78
72
79
- };
73
+ @ Override
74
+ public Object get (Object ctx , String name ) throws Exception {
75
+ return MustacheEnvironmentCollector .this .propertyResolver .getProperty (name );
80
76
}
81
- return null ;
77
+
82
78
}
83
79
84
80
}
0 commit comments