Close

Spring Framework - @Value Examples

Spring Framework 

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import jakarta.annotation.PostConstruct;

@Component
public class MyBean {
@Value("${java.io.tmpdir:/temp}")
private String tempDir;
@Value("${contact-person:Joe}")
private String contactPerson;

@PostConstruct
public void postInit() {
System.out.println("System tempDir: " + tempDir);
System.out.println("contact-person: " + contactPerson);
}
}
Original Post




Using Spring Expression Language with @Value annotation.

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);
}
}
Original Post




In this example, @Value param 'theCurrency' value is auto converted to Currency object. Spring does this implicit conversion using property editors. In this example CurrencyEditor is used behind the scene.

package com.logicbig.example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.Currency;

public class ValueAnnotationExample {

public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(Config.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.doSomething();
}

@Configuration
@PropertySource("classpath:app.properties")
public static class Config {
@Bean
public ClientBean clientBean () {
return new ClientBean();
}
}

public static class ClientBean {
@Value("${theCurrency}")
private Currency currency;

public void doSomething () {
System.out.printf("The currency from prop file is %s%n", currency);
System.out.printf("The currency name is %s%n", currency.getDisplayName());
}
}
}

Output

The currency from prop file is PLN
The currency name is Polish Zloty

src/main/resources/app.properties:

theCurrency=PLN
thePrice=12,323.7654
theTradeDate=2016-9-14
Original Post




This JavaConfig example shows how to use PropertyEditorRegistrar implementation to register a custom editor. Here we use PropertyEditor to convert text from a property file to a Java object.

package com.logicbig.example;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ValueAnnotationExample2 {

public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(Config.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.doSomething();
}

@Configuration
@PropertySource("classpath:app.properties")
public static class Config {

@Bean
public ClientBean clientBean () {
return new ClientBean();
}

@Bean
public static CustomEditorConfigurer customEditorConfigurer () {
CustomEditorConfigurer cec = new CustomEditorConfigurer();
cec.setPropertyEditorRegistrars(
new PropertyEditorRegistrar[]{
new MyCustomBeanRegistrar()});
return cec;
}
}

public static class ClientBean {
@Value("${theTradeDate}")
private Date tradeDate;

public void doSomething () {
System.out.printf("The trade date from prop file is %s%n", tradeDate);
}
}

public static class MyCustomBeanRegistrar implements PropertyEditorRegistrar {
@Override
public void registerCustomEditors (PropertyEditorRegistry registry) {
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
dateFormatter.setLenient(false);
registry.registerCustomEditor(Date.class,
new CustomDateEditor(dateFormatter, true));
}
}
}

src/main/resources/app.properties:

theCurrency=PLN
thePrice=12,323.7654
theTradeDate=2016-9-14
Original Post




See Also