- Spring Essentials
- Shameer Kunjumohamed Hamidreza Sattari
- 125字
- 2021-07-16 13:05:48
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") 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); } … }
推薦閱讀
- Spring 5.0 By Example
- Java EE 6 企業(yè)級應(yīng)用開發(fā)教程
- Java面向?qū)ο筌浖_發(fā)
- 計(jì)算機(jī)圖形學(xué)編程(使用OpenGL和C++)(第2版)
- Hands-On Enterprise Automation with Python.
- Python機(jī)器學(xué)習(xí):手把手教你掌握150個精彩案例(微課視頻版)
- Modular Programming in Java 9
- Active Directory with PowerShell
- 小程序,巧應(yīng)用:微信小程序開發(fā)實(shí)戰(zhàn)(第2版)
- Mudbox 2013 Cookbook
- Python數(shù)據(jù)可視化之matplotlib實(shí)踐
- The Python Apprentice
- PHP面試一戰(zhàn)到底
- Mastering MeteorJS Application Development
- Hands-On Game Development Patterns with Unity 2019