Skip to content

Commit 94050b3

Browse files
committed
Use code includes and tabs in SpEL documentation
See spring-projectsgh-22171
1 parent 62db268 commit 94050b3

File tree

18 files changed

+476
-195
lines changed

18 files changed

+476
-195
lines changed
Lines changed: 10 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -1,219 +1,34 @@
11
[[expressions-beandef]]
22
= Expressions in Bean Definitions
33

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> }`.
246

257
All beans in the application context are available as predefined variables with their
268
common bean name. This includes standard context beans such as `environment` (of type
279
`org.springframework.core.env.Environment`) as well as `systemProperties` and
2810
`systemEnvironment` (of type `Map<String, Object>`) for access to the runtime environment.
2911

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-
6512
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).
6714

6815
The following example sets the default value of a field:
6916

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]
9018

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.
10220

10321
The following example shows the equivalent but on a property setter method:
10422

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]
13724

13825
Autowired methods and constructors can also use the `@Value` annotation, as the following
13926
examples show:
14027

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]
21729

30+
include-code::./MovieRecommender[tag=snippet,indent=0]
21831

32+
You can also refer to other bean properties by name, as the following example shows:
21933

34+
include-code::./ShapeGuess[tag=snippet,indent=0]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.expressions.expressionsbeandef;
18+
19+
public class CustomerPreferenceDao {
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.expressions.expressionsbeandef;
18+
19+
import org.springframework.beans.factory.annotation.Value;
20+
21+
// tag::snippet[]
22+
public class FieldValueTestBean {
23+
24+
@Value("#{ systemProperties['user.region'] }")
25+
private String defaultLocale;
26+
27+
public void setDefaultLocale(String defaultLocale) {
28+
this.defaultLocale = defaultLocale;
29+
}
30+
31+
public String getDefaultLocale() {
32+
return this.defaultLocale;
33+
}
34+
}
35+
// end::snippet[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.expressions.expressionsbeandef;
18+
19+
public class MovieFinder {
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.expressions.expressionsbeandef;
18+
19+
import org.springframework.beans.factory.annotation.Value;
20+
21+
// tag::snippet[]
22+
public class MovieRecommender {
23+
24+
private String defaultLocale;
25+
26+
private CustomerPreferenceDao customerPreferenceDao;
27+
28+
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao,
29+
@Value("#{systemProperties['user.country']}") String defaultLocale) {
30+
this.customerPreferenceDao = customerPreferenceDao;
31+
this.defaultLocale = defaultLocale;
32+
}
33+
34+
// ...
35+
}
36+
// end::snippet[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.expressions.expressionsbeandef;
18+
19+
import org.springframework.beans.factory.annotation.Value;
20+
21+
// tag::snippet[]
22+
public class NumberGuess {
23+
24+
private double randomNumber;
25+
26+
@Value("#{ T(java.lang.Math).random() * 100.0 }")
27+
public void setRandomNumber(double randomNumber) {
28+
this.randomNumber = randomNumber;
29+
}
30+
31+
public double getRandomNumber() {
32+
return randomNumber;
33+
}
34+
}
35+
// end::snippet[]
36+

0 commit comments

Comments
 (0)