Spring Framework FAQ: Top Questions
19. What is PropertySource in Spring and how is it used?
@PropertySource
is an annotation used to declare a property file as a source for externalized configuration in a Spring application context.
πΊοΈ Steps:
- Create a
.properties
file with config values. - Use
@PropertySource
to load it. - Use
@Value
to inject properties.
π₯ Example:
@Configuration
@PropertySource("classpath:appconfig.properties")
public class AppConfig {
@Value("${app.name}")
private String appName;
}
π Expected Output:
app.name property is loaded and injected into the field.
π οΈ Use Cases:
- Loading custom external config files.
- Separating configuration from codebase.