Spring Boot, with its simplicity and convention-over-configuration approach, has become a go-to framework for building robust Java applications. Central to the power of Spring Boot is its ability to seamlessly manage application properties. In this article, we'll delve into three methods for reading properties in Spring Boot apps: using the @Value
annotation, leveraging the Environment
interface, and employing the @ConfigurationProperties
annotation.
1. Using @Value
Annotation
The @Value
annotation provides a straightforward way to inject property values directly into beans. This approach is ideal for injecting individual properties into specific fields. Let's consider an example:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BuildInfoController {
@Value("${build.version}")
private String buildVersion;
@GetMapping("/build-info")
public ResponseEntity<String> getBuildInfo() {
return ResponseEntity.status(HttpStatus.OK).body(buildVersion);
}
}
build:
version: "1.0"
Here, the value of build.version
from application.yml
is injected into the buildVersion
field using the @Value
annotation.
2. Utilizing Environment
Interface
The Environment
interface offers methods to read properties from the application environment. By auto-wiring the Environment
bean, we can easily retrieve property values. Let's see how:
javaCopy codeimport org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class JavaVersionController {
@Autowired
private Environment environment;
@GetMapping("/java-version")
public ResponseEntity<String> getJavaVersion() {
return ResponseEntity.status(HttpStatus.OK).body(environment.getProperty("JAVA_HOME"));
}
}
In this example, we retrieve the value of the JAVA_HOME
property using the getProperty()
method of the Environment
interface.
3. Leveraging @ConfigurationProperties
The @ConfigurationProperties
annotation allows for the binding of an entire group of properties to a bean. By defining a configuration class with annotated fields matching the properties, Spring Boot automatically maps the properties to corresponding fields. Let's illustrate this with an example:
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "accounts")
public record AccountsContactInfoDto(String message, Map<String,String> contactDetails, List<String> onCallSupport) {
}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@RestController
public class AccountsController {
@Autowired
private AccountsContactInfoDto accountsContactInfoDto;
@GetMapping("/contact-info")
public ResponseEntity<AccountsContactInfoDto> getContactInfo(){
return ResponseEntity.status(HttpStatus.OK).body(accountsContactInfoDto);
}
}
@EnableConfigurationProperties(value= {AccountsContactInfoDto.class})
@SpringBootApplicationpublic
class AccountsApplication {
public static void main(String[] args) {
SpringApplication.run(AccountsApplication.class, args);
}
}
accounts:
message: "Welcome to Bank accounts related local APIs "
contactDetails:
name: "Kishore Uputoori - Developer"
email: "Kishoreuputoori@gmail.com"
onCallSupport:
- (816) 927-9883
- (817) 928-9884
In this example, properties with the prefix "accounts"
from application.yml
will be mapped to fields within the AccountsContactInfo
class.
Conclusion:
With these approaches, reading properties in Spring Boot applications becomes a seamless and flexible task, empowering developers to efficiently manage application configurations. Whether it's injecting individual properties, retrieving values from the environment, or binding groups of properties to beans, Spring Boot provides the tools to streamline the process. Harnessing these techniques will undoubtedly enhance the development experience and facilitate the creation of robust Java applications.