Injecting properties into the Spring environment
Besides the separation of environment-specific configuration using profiles, you would still need to externalize many properties, such as database URLs, e-mails, and date formats in a property file for easier handling. These properties would then either be injected directly into the beans or read from environment by the beans at runtime. Spring's environment abstraction, together with @PropertySource
annotation, makes this possible in Spring applications.
The @PropertySource
annotation provides a convenient and declarative mechanism for adding a PropertySource
to Spring's environment:
@Configuration @PropertySource("classpath:application.properties") @ComponentScan(basePackages = "com.springessentialsbook") ...
Get Spring MVC: Designing Real-World Web Applications now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.