INTRODUCTION
We are learning how we can read data from application.properties file. As we store configurations in properties file and they are designed different for different environment. In order to fetch config details in your source code there are some ways motioned below,
Reading values from application.properties :
1) Using @Value Annotation :
The @Value annotation in Spring is used to inject values directly into Spring beans, including properties from the application.properties file. @Value annotation is used to read values from properties file under project class path.
Look at the following example that shows the syntax to read the spring.application.name property value in Java variable by using @Value annotation.
@Value("${spring.datasource.username}")
String username;
Example :
DepartmentController.java
@RequestMapping("/api/departments")
@RestController
public class DepartmentController {
@Value("${spring.datasource.username}")
String username;
@GetMapping("/email")
public String getEmailId() {
return username;
}
}
application.properties
spring.datasource.username=user.
URL : http://localhost:8080/api/departments/email
2) Using Environment object :
Autowiring the Environment object and invoking the getProperty() function to retrieve the value of a property file is another way to access values defined in Spring Boot. The getProperty() method accepts a single necessary parameter, a string holding the property name, and returns its value if it exists. If the property does not exist, the method returns "null".
Example :
DepartmentController.java
@RequestMapping("/api/departments")
@RestController
public class DepartmentController {
@Autowired
Environment environment;
@GetMapping("/email")
public String getEmailId() {
return environment.getProperty("name");
}
}
application.properties
name = suraj
3) Using @ConfigurationProperties annotation :
The @ConfigurationProperties annotation is broader in scope than the @Value annotation and the Environment object. Unlike the previous solutions, this method is used to access related groupings of properties by saving the configuration in a POJO (Plain Old Java Object) and using the POJO for future access. The @ConfigurationProperties annotation will map values based on the group of properties' prefix.
DepartmentController.java
@RequestMapping("/api/departments")
@RestController
public class DepartmentController {
@Autowired
DatabaseConfig databaseConfig;
@GetMapping("/email")
public void getEmailId() {
System.out.println("DatabaseConfig:"+ databaseConfig.getEmail());
System.out.println("DatabaseConfig:"+ databaseConfig.getPassword());
System.out.println("DatabaseConfig:"+ databaseConfig.getUsername());
}
}
DatabaseConfig.class
@ConfigurationProperties(prefix="spring.datasource")
@Configuration
@Data
public class DatabaseConfig {
public String username;
public String password;
public String email;
}
application.properties
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.email=surajladda07@email.com
CONCLUSION :
We have learned three ways to read properties file. So among three ways @Value annotation is widely used. Environment object is not used often but it is also way to read the property file. @ConfigurationProperties is also a way but It can be used in bigger groups of properties file.
USEFUL LINKS:
0 Comments
Post a Comment