- Spring SpEL - Home
- Spring SpEL - Overview
- Spring SpEL - Environment Setup
- Spring SpEL - Create Project
- Spring SpEL - Literal Expression
- Spring SpEL - Properties
- Spring SpEL - Array
- Spring SpEL - List
- Spring SpEL - Map
- Spring SpEL - Methods
- Spring SpEL - Relational Operators
- Spring SpEL - Logical Operators
- Spring SpEL - Mathematical Operators
- Spring SpEL - Assignment Operator
- Spring SpEL - Constructor
- Spring SpEL - Variables
- Spring SpEL - Functions
- Spring SpEL - Expression Templating
Spring SpEL Expression Evaluation
Spring SpEL Bean Configuration
Spring SpEL Language Reference
Spring SpEL Operators
Spring SpEL Special Operators
Spring SpEL Collections
Spring SpEL Other Features
Spring SpEL - Useful Resources
Spring SpEL - Annotation Based Configuration
SpEL expression can be used in Annotation based beans configuration
Syntax
Following is an example of using an expression in annotation based configuration.
@Value("#{ T(java.lang.Math).random() * 100.0 }")
private int id;
Here we are using @Value annotation and we've specified a SpEL expression on a property. Similarly we can specify SpEL expression on setter methods, on constructors and during autowiring as well.
@Value("#{ systemProperties['user.country'] }")
public void setCountry(String country) {
this.country = country;
}
Following example shows the various use cases.
Example - Usage of SpEL in Annotation based configuration
Let's update the project created in Spring SpEL - Create Project chapter. We're adding/updating following files −
Employee.java − An employee class.
AppConfig.java − A configuration class.
MainApp.java − Main application to run and test.
Employee.java
Here is the content of Employee.java file −
package com.tutorialspoint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Employee {
@Value("#{ T(java.lang.Math).random() * 100.0 }")
private int id;
private String name;
private String country;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
@Value("Mahesh")
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
@Value("#{ systemProperties['user.country'] }")
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "[" + id + ", " + name + ", " + country + "]";
}
}
AppConfig.java
Here is the content of MainApp.java file −
package com.tutorialspoint;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.tutorialspoint")
public class AppConfig {
}
MainApp.java
Here is the content of MainApp.java file −
package com.tutorialspoint;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainApp {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(AppConfig.class);
context.refresh();
Employee emp = context.getBean(Employee.class);
System.out.println(emp);
}
}
Output
Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message −
[50, Mahesh, IN]