You can access property values quickly from the /resources/application.property file simply by using the @Value annotation.
@Value allows you to quickly connect your code to a property value.
In your application.properties file just add a new value as follows:
server.port=8100
my.value=Hello World
Here we have added a new property called my.value and set the value to Hello World. Note that we do not need quotes for strings.
This is the same file that we set properties like server.port to change the port on Spring Boot listens on for Http end points.
Now that we have the value in the properties file can access in our code as follows:
public class ValueRestController {
@Value("${my.value}")
private String myValue;
@GetMapping("/myvalue")
public String myValue() {
log.info("myValue=" + myValue);
return myValue;
}
}
Now we have the value “Hello World” injected into the String myValue and accessible in the application.
Checkout the code and video on YouTube
Follow me please
I’ve lived in California all my life. I grew up in Silicon Valley and have seen so many changed.
Please feel free to follow me on Medium and YouTube, I have classes on Udemy and just started a new Series called “Fast and Simple Development” where you can learn some new technologies and skills that are needed to help upskill yourself in today’s job market or simply continue to make the move up the ladder by adding to your technical chops.
https://www.youtube.com/@fastandsimpledevelopment?sub_confirmation=1
https://www.udemy.com/user/tomjay2