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") public class SpringJavaConfigurator { ... @Autowired @Lazy private SystemSettings systemSettings; @Autowired private Environment env; @Bean public SystemSettings getSystemSettings() { String dateFormat = env.getProperty("system.date-format"); String appDisplayName = env.getProperty("app.displayname"); return new SystemSettings(dateFormat, appDisplayName); } … }