- 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); } … }
推薦閱讀
- Vue.js 3.x快速入門
- Mastering NetBeans
- PyTorch自動駕駛視覺感知算法實戰
- Full-Stack React Projects
- Building Minecraft Server Modifications
- iOS編程基礎:Swift、Xcode和Cocoa入門指南
- C#程序設計基礎:教程、實驗、習題
- 自制編程語言
- Mastering ROS for Robotics Programming
- PHP+Ajax+jQuery網站開發項目式教程
- Natural Language Processing with Java and LingPipe Cookbook
- Spring Boot+Vue全棧開發實戰
- ScratchJr趣味編程動手玩:讓孩子用編程講故事
- Java程序設計與項目案例教程
- ASP.NET求職寶典