This example shows how to use spring expression language in value element of @Value.
Example
Using expression language
package com.logicbig.example;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import jakarta.annotation.PostConstruct;
@Component
public class MyBean {
@Value("#{systemProperties['user.home']}")
private String userHome;
@Value("#{T(java.lang.Math).random()*1000}")
private int randomNumber;
@PostConstruct
public void postInit() {
System.out.println("System userHome: " + userHome);
System.out.println("Random number: " + randomNumber);
}
}
Main class
package com.logicbig.example;
import org.springframework.context.annotation.*;
@Configuration
@ComponentScan
public class ExampleMain {
public static void main(String[] args) {
new AnnotationConfigApplicationContext(ExampleMain.class);
}
}
Output
System userHome: C:\Users\Joe Random number: 327
Example Project
Dependencies and Technologies Used:
spring-context 6.1.2 (Spring Context) Version Compatibility: 3.2.9.RELEASE - 6.1.2Version List
×
Version compatibilities of spring-context with this example: