@Value in Spring Boot

In this article, we will see about the spring boot annotation @Value. The annotation @Value is used to automatically assign a value from multiple environment such as spring environment, system environment, property files, bean objects, default value etc.

The annotation @Value in spring boot is commonly used to inject the configuration values to the spring boot application. Let’s see the examples how to use @Value annotation for injecting values.



@Value with default value

@Value with default string value – The argument of the annotation @Value can be a java string.

	@Value("Yawin")
	private String name;

@Value with default integer value – The argument of the annotation @Value is a string contains a number such as integer, long.

	@Value("25")
	private int age;

@Value with default double value – The argument of the annotation @Value is a string contains decimal values such as float, double

	@Value("75.25")
	private double price;

@Value with default boolean value – A boolean string value can be an argument for an annotation @Value. The values are true and false.

	@Value("true")
	private boolean isReadOnly;

@Value with default date value – A date can be set as default value in the annotation @Value in spring boot. The java date format has to be specified to parse the date.

	@DateTimeFormat(pattern = "MM-dd-yyyy")
	@Value("06-21-2003")
	private Date dateOfBirth;

@Value with default Calendar value – The date can be set as java default calendar object in the annotation @Value in spring boot.

	@DateTimeFormat(pattern = "MM-dd-yyyy")
	@Value("06-21-2003")
	private Calendar dateOfBirth;

@Value with default value as null – The default value can be set as null explicitly in the annotation @Value.

	@Value("#{null}")
	private String name;


@Value with Properties files

The annotation @Value is used to inject values from the properties file. The annotation @PropertySource is used to read the properties and inject in the annotation @Value.

application.properties

source.file.name=test.txt

@Value with reading a property from application.properties – The below example will allow to read a property from the application.properties

	@Value("${source.file.name}")
	private String sourceFileName;

@Value with reading a property if not use default value – The property value is injected from the property file. If the property is not set, read a default value. In this example the service port is not available in property file. So the default value 80 is assigned in the variable servicePort.

	@Value("${service.port:80}")
	private String servicePort;


@Value with System Environment

The annotation @Value is used to inject the system environment variable values. The system environment variables are set in the operating system. These variables are common across all the application in the system.

@Value with system environment – The system environment variable is added in the annotation @Values to read value from the system environment

	@Value("${java.home}")
	private String javaHome;

check the value in the command line or system environment properties
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home


@Value with Spring Environment

As like system environment, spring environment maintains a list of values in key value pair. The spring environments are added as postfix with the properties files. The application.properties is created in multiple copies based on the environment such as application-prod.properties, application-test.properties, application-perf.properties etc.Based on the spring environment, the corresponding properties files will be loaded in spring environment.

@Value with Spring Environment value – The annotation @Value is used to read value from the spring environment based on the profile selected to run.

	@Value("${source.file.name}")
	private String sourceFileName;


@Value with priorty order

The annotation @Value will read the value based on the priority order. The first priority is from the system environment, second priority is from spring environment, third priority is from properties file and the last priority is the default value assigned to the variable.

	@Value("${source.file.name:example.txt}")
	private String sourceFileName;

Priority order
   System environment
   Spring environment
   Properties files configured in @PropertySource
   application.properties
   default value assigned


@Value with Spring Expression (SpEL)

The spring expression language is an expression language that provides expression evaluation capability with in the spring boot framework. The expression created within the java string is parsed, evaluated and execute and returns the result. The spring expression language (SpEL) always enclosed with a curly brace ( ‘{‘ ‘}’) prefixed with a hash symbol (#).

	@Value("#{systemProperties['java.home']}")
	private String javaHome;


@Value with Method Global Value

The annotation @Value is used to inject value to all the parameters of the method. All parameters of a method is assigned with the same value in spring boot.

	@Bean
	@Value("${manager.name}")
	private Employee getEmployee(String name, String manager){
		Employee emp = new Employee();
		emp.setName(name);
		emp.setManager(manager);
		return emp;
	}

In the above example, the employee object prints the same value in both emp.getName() and emp.getManager() methods.



@Value with Method Parameter Value

The value injected as global will not have meaning in most of the real time scenarios. The individual values for each method parameter should be set in most of the cases. The annotation @Value allows to inject values in the method parameter.

	@Bean
	private Employee getEmployee(@Value("${employee.name}") String name, @Value("${manager.name}") String manager){
		Employee emp = new Employee();
		emp.setName(name);
		emp.setManager(manager);
		return emp;
	}


@Value with Constructor method

The annotation @Value can inject values in constructor while creating the instance of a class in the bean.

	@Component
	public class EmployeeService {
		public EmployeeService(@Value("${company.name}") String name) {
			......
		}
	}



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *