To configure a custom class with a Spring @Value property, you need to follow the below steps:
public class CustomClass {
private String endpointUrl;
private String apiKey;
// constructor and getters/setters
}
@Component
public class MyConfig {
@Value("${api.endpoint}")
private String endpointUrl;
@Value("${api.key}")
private String apiKey;
@Bean
public CustomClass customClass() {
return new CustomClass(endpointUrl, apiKey);
}
}
@Autowired
private CustomClass customClass;
// use customClass instance variables as required
In the example above, we used @Value to load the values for endpointUrl and apiKey from properties files. You can also use other sources such as system properties or environment variables.
Asked: 2023-05-25 11:48:50 +0000
Seen: 1 times
Last updated: May 25 '23