Spring @Value annotation tricks

Spring project initializer page (https://start.spring.io/)
The structure of the project. (Using IntelliJ IDE)
ValueControler.java class
package com.habeebcycle.springvalueannotation.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ValueController {

@Value("Hello World")
private String greetingMessage;

@GetMapping("")
public String sendGreeting(){
return greetingMessage;
}
}
@Value("1.234")
private double doubleValue; //could be Double
@Value("1234")
private Integer intValue; //could be int
@Value("true")
private boolean boolValue; //could be Boolean
@Value("2000")
private long longValue;
#application.propertiesgreeting.message=Hello World!
package com.habeebcycle.springvalueannotation.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ValueController {

@Value("${greeting.message}")
private String greetingMessage;

@GetMapping("")
public String sendGreeting(){
return greetingMessage;
}
}
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-02-29 21:54:43.953 ERROR 2996 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'valueController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'greeting.message' in value "${greeting.message}"
package com.habeebcycle.springvalueannotation.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ValueController {

@Value("${greeting.message:Greeting not found!}")
private String greetingMessage;

@GetMapping("")
public String sendGreeting(){
return greetingMessage;
}
}
#application.propertiesmy.int.value=20
my.double.value=3.142
my.boolean.value=true
//In the Java file@Value("${my.int.value:0}")
private int intValue; //injects 20 at runtime
@Value("${my.double.value: 0.0}")
private double doubleValue; //injects 3.142 at runtime
//This takes the default value provided despite having the property //name in the properties file.
@Value("${my.boolean.value :false}")
private boolean boolValue; //injects false because of the space
//Property name not found in the properties file
@Value("${my.long.value:300}")
private long longValue; //injects 300 at runtime
#application.propertiesmy.weekdays=Mon,Tue,Wed,Thu,Fri//In the Java file@Value("${my.weekdays}")
private List<String> strList; // injects [Mon, Tue, Wed, Thu, Fri]
//Providing default value
@Value("${my.weekends:Sat,Sun,Fri}")
private List<String> strList2; // injects [Sat, Sun, Fri]
#application.propertiesdatabase.values={url: 'http://127.0.0.1:3306/', db: 'mySql', username: 'root', password: 'root'}
package com.habeebcycle.springvalueannotation.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class ValueController {

@Value("#{${database.values}}")
private Map<String, String> dbValues;

@GetMapping("")
public Map getDBProps(){
return dbValues;
}
}
{
"url": "http://127.0.0.1:3306/",
"db": "mySql",
"username": "root",
"password": "root"
}
@Value("#{${database.values: {url: 'http://127.0.0.1:3308/', db: 'mySql', username: 'root', password: ''}}}")
private Map<String, String> dbValues;
company.name= Scopesuite Pty ltd
#company.location= Sydney
package com.habeebcycle.springvalueannotation.service;import org.springframework.stereotype.Service;@Service
public class CompanyService {
private String compName;
private String location;

public CompanyService(@Value("${company.name}") String compName,
@Value("${company.location:Washington}") String location){

this.compName = compName;
this.location = location;
}
------
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Habeeb Okunade

Habeeb Okunade

Innovative software development professional. Loves sharing knowledge and learning latest stacks. Expertise in microservices architecture using spring boot.