- 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); } … }
推薦閱讀
- Visual FoxPro程序設計教程
- Three.js開發指南:基于WebGL和HTML5在網頁上渲染3D圖形和動畫(原書第3版)
- Getting Started with Greenplum for Big Data Analytics
- iOS開發實戰:從入門到上架App Store(第2版) (移動開發叢書)
- WordPress 4.0 Site Blueprints(Second Edition)
- SQL Server數據庫管理與開發兵書
- NetBeans IDE 8 Cookbook
- C++20高級編程
- JavaScript程序設計(第2版)
- Hands-On Full Stack Development with Spring Boot 2.0 and React
- Illustrator CS6設計與應用任務教程
- Python Social Media Analytics
- 從零開始學Unity游戲開發:場景+角色+腳本+交互+體驗+效果+發布
- Mastering Object:Oriented Python(Second Edition)
- 計算機程序的構造和解釋(JavaScript版)