You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Token expressions are simple expressions used to directly access specific data (Beans, attributes, parameters, etc.) within Aspectran's configuration. Each token has a defined syntax and role.
12
+
Token expressions are simple yet powerful placeholders used to directly access specific data (Beans, attributes, parameters, etc.) within Aspectran's configuration. They act as bridges connecting your configuration to the runtime data.
13
13
14
-
| Token Type | Expression Syntax | Description |
15
-
| :--- | :--- | :--- |
16
-
|**Bean**|`#{beanId}`| References the Bean object with the ID `beanId`. |
17
-
|**Attribute**|`@{attributeName}`| References the `attributeName` attribute of the current request.|
18
-
|**Parameter**|`${parameterName}`| References the `parameterName` parameter of the current request.|
19
-
|**Property**|`%{propertyName}`| References the `propertyName` environment property of the application.|
20
-
|**Template**|`~{templateId}`| Renders the template with the ID `templateId` and **includes the result**. |
14
+
| Token Type | Expression Syntax | Description | Usage Example |
15
+
| :--- | :--- | :--- | :--- |
16
+
|**Bean**|`#{beanId}`| References the Bean object with the ID `beanId`. |`#{userService}`|
17
+
|**Attribute**|`@{attributeName}`| References the `attributeName` attribute of the current request context. Attributes are often used to pass data between actions. |`@{userInfo}`|
18
+
|**Parameter**|`${parameterName}`| References the `parameterName` parameter of the current request (e.g., HTTP query parameter). |`${userId}`|
19
+
|**Property**|`%{propertyName}`| References the `propertyName` environment property (e.g., from properties files or system properties). |`%{app.uploadDir}`|
20
+
|**Template**|`~{templateId}`| Renders the template with the ID `templateId` and **includes the result**. |`~{emailTemplate}`|
21
21
22
22
### 1.1. Property Accessor: `^`
23
23
24
-
To access a specific property (Getter) of an object referenced by a token expression, use the `^` separator instead of `.`.
24
+
To access a specific property (Getter) of an object referenced by a token expression, use the `^` separator instead of `.`. This is a unique feature of Aspectran to distinguish between dot-delimited IDs and property access.
25
25
26
26
***Syntax**: `#{beanId^propertyName}`
27
-
***Explanation**: If you use `.`, the entire `bean.property` is recognized as a single ID. However, using `^` finds the `propertyName` property from the object referenced by the `beanId` token.
***Explanation**: If you use `.`, Aspectran interprets `bean.property` as the full ID of the bean. Using `^` explicitly tells Aspectran to "get the bean first, then access its property".
28
29
29
30
### 1.2. Setting Default Values
30
31
31
-
You can use the `:` separator to specify a default value to be used if a parameter or attribute is not present. This feature is mainly used within the `<item>` tag.
32
+
You can use the `:` separator to specify a default value to be used if a parameter or attribute is null or missing. This prevents runtime errors and simplifies configuration.
32
33
33
34
```xml
34
35
<attributes>
35
-
<!-- If the 'name' parameter is not present, use "Jane" as the default value -->
36
-
<itemname="name">${name:Jane}</item>
36
+
<!-- If 'page' parameter is missing, default to "1" -->
37
+
<itemname="page">${page:1}</item>
38
+
39
+
<!-- If 'sort' parameter is missing, default to "desc" -->
40
+
<itemname="sort">${sort:desc}</item>
37
41
</attributes>
38
42
```
39
43
40
44
### 1.3. Token Directives
41
45
42
-
You can specify the source of a value within a token expression using a colon (`:`). These are called token directives, and you can use the types defined in `TokenDirectiveType`.
46
+
You can specify the source of a value within a token expression using a colon (`:`). These are called token directives.
43
47
44
48
| Directive | Description | Example |
45
49
| :--- | :--- | :--- |
46
-
|**`field`**| References the value of a static field. |`#{field:com.example.Constant^STATIC_FIELD}`|
50
+
|**`field`**| References the value of a static field. |`#{field:java.awt.Color^RED}`|
47
51
|**`method`**| Calls a static method and uses its return value. |`#{method:java.lang.System^currentTimeMillis}`|
48
52
|**`class`**| References a static property (getter) of a class. |`#{class:java.io.File^separator}`|
49
-
|**`classpath`**| References a property from a resource on the classpath (usually a .properties file). |`%{classpath:config/app.properties^db.url}`|
50
-
|**`system`**| References a Java System Property value. |`%{system:java.version}`|
53
+
|**`classpath`**| References a property from a resource on the classpath. |`%{classpath:config/jdbc.properties^jdbc.url}`|
54
+
|**`system`**| References a Java System Property value. |`%{system:user.home}`|
51
55
52
56
## 2. AsEL Expressions (Token Expressions + OGNL)
53
57
54
-
AsEL expressions are used by combining the token expressions described above with OGNL expressions. This enables dynamic data processing and operations that go beyond simple value references. They can be used freely in places like the `@Value` annotation or templates.
58
+
AsEL expressions combine the simplicity of token expressions with the power of OGNL (Object-Graph Navigation Language). This enables dynamic data processing, mathematical operations, and complex conditional logic directly in your configuration.
@Value("@{userList}[0]") // First user in the list
92
+
publicUser firstUser;
93
+
94
+
@Value("@{configMap}['timeout']") // Value for key 'timeout'
95
+
publicint timeout;
96
+
```
97
+
98
+
### 2.1. StringConcatenationRules
99
+
100
+
When an AsEL expression contains multiple tokens or a mix of literal text and tokens, the entire string is evaluated as a single OGNL expression. Therefore, you must strictly follow OGNL syntax, such as using single quotes (`'`) for literal text and the `+` operator for concatenation.
In this case, `@{user^name}` and `${userId}` are substituted with OGNL variables (e.g., `#__var1__`), and OGNL evaluates the combined string successfully.
67
107
68
-
***PerformingConditionalLogic with the Value of a TokenExpression**
108
+
* **Incorrect Example**:
69
109
```java
70
-
@Value("%{app.mode} == 'development'")
71
-
publicboolean isDevelopmentMode;
110
+
@Value("User: @{user^name} (ID: ${userId})")
72
111
```
112
+
This will cause an `ExpressionParserException` because `User:` and `(ID:` are not valid OGNL syntax when used without quotes and operators.
73
113
74
114
## 3. Template Usage
75
115
@@ -87,8 +127,8 @@ The `~{...}` token is used to render the template with the specified ID at the c
87
127
```xml
88
128
<!--Built-in template rule definition -->
89
129
<template id="welcomeMailTemplate" style="apon">
90
-
|Hello, @{user^name}! Welcome to our service.
91
-
|Your current point balance is #{pointBean^currentPoints}.
130
+
|Hello, @{user^name}!Welcome to our service.
131
+
|Your current point balance is #{pointBean^currentPoints}.
92
132
</template>
93
133
94
134
<!--Use the template by transforming it elsewhere -->
@Value("@{configMap}['timeout']") // 'timeout' 키에 해당하는 값
95
+
publicint timeout;
66
96
```
67
97
68
-
***토큰 표현식의 값으로 조건부 로직 수행**
98
+
### 2.1. 문자열 결합 규칙
99
+
100
+
AsEL 표현식에 여러 개의 토큰이 포함되거나 일반 텍스트와 토큰이 혼합된 경우, 전체 문자열은 **하나의 OGNL 표현식**으로 평가됩니다. 따라서 일반 텍스트에는 작은따옴표(`'`)를 사용하고 결합에는 `+` 연산자를 사용하는 등 OGNL 문법을 엄격히 따라야 합니다.
0 commit comments