|
1 | 1 | [[expressions-beandef]]
|
2 | 2 | = Expressions in Bean Definitions
|
3 | 3 |
|
4 |
| -You can use SpEL expressions with XML-based or annotation-based configuration metadata for |
5 |
| -defining `BeanDefinition` instances. In both cases, the syntax to define the expression is of the |
6 |
| -form `#{ <expression string> }`. |
7 |
| - |
8 |
| - |
9 |
| - |
10 |
| -[[expressions-beandef-xml-based]] |
11 |
| -== XML Configuration |
12 |
| - |
13 |
| -A property or constructor argument value can be set by using expressions, as the following |
14 |
| -example shows: |
15 |
| - |
16 |
| -[source,xml,indent=0,subs="verbatim"] |
17 |
| ----- |
18 |
| - <bean id="numberGuess" class="org.spring.samples.NumberGuess"> |
19 |
| - <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/> |
20 |
| -
|
21 |
| - <!-- other properties --> |
22 |
| - </bean> |
23 |
| ----- |
| 4 | +You can use SpEL expressions with configuration metadata for defining bean instances. In both |
| 5 | +cases, the syntax to define the expression is of the form `#{ <expression string> }`. |
24 | 6 |
|
25 | 7 | All beans in the application context are available as predefined variables with their
|
26 | 8 | common bean name. This includes standard context beans such as `environment` (of type
|
27 | 9 | `org.springframework.core.env.Environment`) as well as `systemProperties` and
|
28 | 10 | `systemEnvironment` (of type `Map<String, Object>`) for access to the runtime environment.
|
29 | 11 |
|
30 |
| -The following example shows access to the `systemProperties` bean as a SpEL variable: |
31 |
| - |
32 |
| -[source,xml,indent=0,subs="verbatim"] |
33 |
| ----- |
34 |
| - <bean id="taxCalculator" class="org.spring.samples.TaxCalculator"> |
35 |
| - <property name="defaultLocale" value="#{ systemProperties['user.region'] }"/> |
36 |
| -
|
37 |
| - <!-- other properties --> |
38 |
| - </bean> |
39 |
| ----- |
40 |
| - |
41 |
| -Note that you do not have to prefix the predefined variable with the `#` symbol here. |
42 |
| - |
43 |
| -You can also refer to other bean properties by name, as the following example shows: |
44 |
| - |
45 |
| -[source,xml,indent=0,subs="verbatim"] |
46 |
| ----- |
47 |
| - <bean id="numberGuess" class="org.spring.samples.NumberGuess"> |
48 |
| - <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/> |
49 |
| -
|
50 |
| - <!-- other properties --> |
51 |
| - </bean> |
52 |
| -
|
53 |
| - <bean id="shapeGuess" class="org.spring.samples.ShapeGuess"> |
54 |
| - <property name="initialShapeSeed" value="#{ numberGuess.randomNumber }"/> |
55 |
| -
|
56 |
| - <!-- other properties --> |
57 |
| - </bean> |
58 |
| ----- |
59 |
| - |
60 |
| - |
61 |
| - |
62 |
| -[[expressions-beandef-annotation-based]] |
63 |
| -== Annotation Configuration |
64 |
| - |
65 | 12 | To specify a default value, you can place the `@Value` annotation on fields, methods,
|
66 |
| -and method or constructor parameters. |
| 13 | +and method or constructor parameters (or XML equivalent). |
67 | 14 |
|
68 | 15 | The following example sets the default value of a field:
|
69 | 16 |
|
70 |
| -[tabs] |
71 |
| -====== |
72 |
| -Java:: |
73 |
| -+ |
74 |
| -[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
75 |
| ----- |
76 |
| - public class FieldValueTestBean { |
77 |
| -
|
78 |
| - @Value("#{ systemProperties['user.region'] }") |
79 |
| - private String defaultLocale; |
80 |
| -
|
81 |
| - public void setDefaultLocale(String defaultLocale) { |
82 |
| - this.defaultLocale = defaultLocale; |
83 |
| - } |
84 |
| -
|
85 |
| - public String getDefaultLocale() { |
86 |
| - return this.defaultLocale; |
87 |
| - } |
88 |
| - } |
89 |
| ----- |
| 17 | +include-code::./FieldValueTestBean[tag=snippet,indent=0] |
90 | 18 |
|
91 |
| -Kotlin:: |
92 |
| -+ |
93 |
| -[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] |
94 |
| ----- |
95 |
| - class FieldValueTestBean { |
96 |
| -
|
97 |
| - @Value("#{ systemProperties['user.region'] }") |
98 |
| - var defaultLocale: String? = null |
99 |
| - } |
100 |
| ----- |
101 |
| -====== |
| 19 | +Note that you do not have to prefix the predefined variable with the `#` symbol here. |
102 | 20 |
|
103 | 21 | The following example shows the equivalent but on a property setter method:
|
104 | 22 |
|
105 |
| -[tabs] |
106 |
| -====== |
107 |
| -Java:: |
108 |
| -+ |
109 |
| -[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
110 |
| ----- |
111 |
| - public class PropertyValueTestBean { |
112 |
| -
|
113 |
| - private String defaultLocale; |
114 |
| -
|
115 |
| - @Value("#{ systemProperties['user.region'] }") |
116 |
| - public void setDefaultLocale(String defaultLocale) { |
117 |
| - this.defaultLocale = defaultLocale; |
118 |
| - } |
119 |
| -
|
120 |
| - public String getDefaultLocale() { |
121 |
| - return this.defaultLocale; |
122 |
| - } |
123 |
| - } |
124 |
| ----- |
125 |
| -
|
126 |
| -Kotlin:: |
127 |
| -+ |
128 |
| -[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] |
129 |
| ----- |
130 |
| - class PropertyValueTestBean { |
131 |
| -
|
132 |
| - @Value("#{ systemProperties['user.region'] }") |
133 |
| - var defaultLocale: String? = null |
134 |
| - } |
135 |
| ----- |
136 |
| -====== |
| 23 | +include-code::./PropertyValueTestBean[tag=snippet,indent=0] |
137 | 24 |
|
138 | 25 | Autowired methods and constructors can also use the `@Value` annotation, as the following
|
139 | 26 | examples show:
|
140 | 27 |
|
141 |
| -[tabs] |
142 |
| -====== |
143 |
| -Java:: |
144 |
| -+ |
145 |
| -[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
146 |
| ----- |
147 |
| - public class SimpleMovieLister { |
148 |
| -
|
149 |
| - private MovieFinder movieFinder; |
150 |
| - private String defaultLocale; |
151 |
| -
|
152 |
| - @Autowired |
153 |
| - public void configure(MovieFinder movieFinder, |
154 |
| - @Value("#{ systemProperties['user.region'] }") String defaultLocale) { |
155 |
| - this.movieFinder = movieFinder; |
156 |
| - this.defaultLocale = defaultLocale; |
157 |
| - } |
158 |
| -
|
159 |
| - // ... |
160 |
| - } |
161 |
| ----- |
162 |
| -
|
163 |
| -Kotlin:: |
164 |
| -+ |
165 |
| -[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] |
166 |
| ----- |
167 |
| - class SimpleMovieLister { |
168 |
| -
|
169 |
| - private lateinit var movieFinder: MovieFinder |
170 |
| - private lateinit var defaultLocale: String |
171 |
| -
|
172 |
| - @Autowired |
173 |
| - fun configure(movieFinder: MovieFinder, |
174 |
| - @Value("#{ systemProperties['user.region'] }") defaultLocale: String) { |
175 |
| - this.movieFinder = movieFinder |
176 |
| - this.defaultLocale = defaultLocale |
177 |
| - } |
178 |
| -
|
179 |
| - // ... |
180 |
| - } |
181 |
| ----- |
182 |
| -====== |
183 |
| - |
184 |
| -[tabs] |
185 |
| -====== |
186 |
| -Java:: |
187 |
| -+ |
188 |
| -[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
189 |
| ----- |
190 |
| - public class MovieRecommender { |
191 |
| -
|
192 |
| - private String defaultLocale; |
193 |
| -
|
194 |
| - private CustomerPreferenceDao customerPreferenceDao; |
195 |
| -
|
196 |
| - public MovieRecommender(CustomerPreferenceDao customerPreferenceDao, |
197 |
| - @Value("#{systemProperties['user.country']}") String defaultLocale) { |
198 |
| - this.customerPreferenceDao = customerPreferenceDao; |
199 |
| - this.defaultLocale = defaultLocale; |
200 |
| - } |
201 |
| -
|
202 |
| - // ... |
203 |
| - } |
204 |
| ----- |
205 |
| -
|
206 |
| -Kotlin:: |
207 |
| -+ |
208 |
| -[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] |
209 |
| ----- |
210 |
| - class MovieRecommender(private val customerPreferenceDao: CustomerPreferenceDao, |
211 |
| - @Value("#{systemProperties['user.country']}") private val defaultLocale: String) { |
212 |
| - // ... |
213 |
| - } |
214 |
| ----- |
215 |
| -====== |
216 |
| - |
| 28 | +include-code::./SimpleMovieLister[tag=snippet,indent=0] |
217 | 29 |
|
| 30 | +include-code::./MovieRecommender[tag=snippet,indent=0] |
218 | 31 |
|
| 32 | +You can also refer to other bean properties by name, as the following example shows: |
219 | 33 |
|
| 34 | +include-code::./ShapeGuess[tag=snippet,indent=0] |
0 commit comments