Spring Property Injection Concepts: Difference between revisions
Line 54: | Line 54: | ||
TODO | TODO | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Configuration Property Metadata== | |||
==Property Injection and Auto-Configuration== | ==Property Injection and Auto-Configuration== |
Revision as of 21:39, 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. The environment abstracts the origins of properties and applies precedence rules when the same property is specified in more than one source. It pulls property values from several sources: Java system properties, command-line arguments, environment variables, configuration files, etc. The property sources are described in detail in their corresponding sections, below.
TODO: Environment can be injected and inspected.
Injecting Properties into Beans
Configuration Property Holders
A common pattern used to handle injected configuration is to declare a configuration property holder class, whose sole purpose in the application is to be holder of configuration data. Such class bundles several configuration properties together, under a common property namespace. There is nothing special about configuration property holders. They are ordinary Spring components that have their properties injected from the Spring environment. They can be injected inot any other bean that needs those properties. One benefit of this approach is that it keeps configuration-specific details out of controllers and other application specific classes. Another benefit is that it makes it easy to share common configuration properties among several beans that may need this information. All configuration properties related to a certain piece of functionality are kept in a single place, as opposite to being scattered across several components, and if we need to add a new property or change an existing property, we do it in a single place.
Configuration property holder classes are a good location to apply JavaBeans Validation annotations.
@Component
@ConfigurationProperties(prefix = "playground.spring.pi.example01")
@Data
@Validated
public class MyPropertyConfiguration {
public static final int DEFAULT_SIZE = 20;
public static final String DEFAULT_COLOR = "blue";
@Min(value = 5, message = "the size must be larger or equal than 5")
@Max(value = 40, message = "the size must be smaller or equal than 40")
private int size = DEFAULT_SIZE;
private String color = DEFAULT_COLOR;
}
Conventionally, hardcoded defaults are specified as initialization values for the member variables.
The full playground project is available here:
Exposing Individual Configuration Properties on Components
Individual properties can also be injected directly into established components, as shown below. However, if you have the option, bundling configuration properties into configuration property holders is generally a better approach, for the reasons presented above.
TODO
Configuration Property Metadata
Property Injection and Auto-Configuration
The beans that are automatically configured by the Spring Boot autoconfiguration. process are all configurable by properties drawn from the Spring environment.
Property Sources
Java System Properties
Command-Line Arguments
java -jar ... --<property.name>=<value>
Example:
java -jar ... --server.port=9999
Environment Variables
The naming style should accommodate restrictions placed on environment variable names by the operating system.
export SERVER_PORT=9999
Property Configuration Files
application.properties
Configuration Service
The configuration service is also referred to as configuration server.
Precedence
Profiles
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.
- https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#configuration-metadata-annotation-processor