Spring Property Injection Concepts: Difference between revisions
Line 13: | Line 13: | ||
=The Environment Abstraction= | =The Environment Abstraction= | ||
The Spring environment is the only source of [[#Configuration_Property|configuration properties]] for components needing them. It abstracts the origins of properties and applies precedence rules when the same property is specified in more than one source. | The Spring environment is the only source of [[#Configuration_Property|configuration properties]] for components needing them. It abstracts the [[#Property_Sources|origins of properties]] and applies [[#Precedence|precedence rules]] when the same property is specified in more than one source. | ||
=Injecting Properties into Beans= | =Injecting Properties into Beans= |
Revision as of 19:49, 21 November 2018
Internal
Overview
Spring has two different, but related kinds of configurations: bean wiring, which refers to declaring application components and their dependencies, and how those dependencies should be injected into each other, as described in the Dependency Injection and Inversion of Control Container section, and property injection, which is the process that allows external pieces of data, known as configuration properties, to be provided to the application runtime at startup or while it is running, in form of Java system properties, environment variables and by other means. This section addresses property injection concepts.
Configuration Properties
Configuration properties are pieces of data coded into Spring components using the JavaBeans property conventions. They usually have a corresponding member variable, a getter and a setter. The Spring Framework provides conventions and mechanisms to automatically inject values into configuration properties, while those values come from several different property sources.
The Environment Abstraction
The Spring environment is the only source of configuration properties for components needing them. It abstracts the origins of properties and applies precedence rules when the same property is specified in more than one source.
Injecting Properties into Beans
Property Injection and Auto-Configuration
Property Sources
Java System Properties
Command-Line Arguments
Environment Variables
Property Files
Configuration Service
Precedence
Profiles
application.properties
Application Arguments
Environment Variables
Profile
TODO
- Diagram with property sources and their relative priority, including the configuration service (chapter 14).
- Augment https://kb.novaordis.com/index.php/Spring_Boot_Concepts#Autoconfiguration with specialized sections on property injection for DataSources, etc.
- How do I read the effective values of all configuration properties during testing and at runtime?
- You can figure out the active profile from within a Spring application by @Autowire-ing org.springframework.core.env.Environment and inspecting it.