Spring Property Injection Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 23: Line 23:


'''Configuration properties''' are pieces of data coded into Spring components using the [[JavaBeans#Overview|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|property sources]].
'''Configuration properties''' are pieces of data coded into Spring components using the [[JavaBeans#Overview|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|property sources]].
The environment provides the user with a convenient service interface for configuring property sources and resolving properties from them.


The Spring [[#Environment|environment]] is the only source of [[#Configuration_Property|configuration properties]] for components needing them. The environment abstracts the [[#Property_Sources|origins of properties]] and applies [[#Precedence|precedence rules]] when the same property is specified in more than one source. It pulls property values from several sources: [[#Java_System_Properties|Java system properties]], [[#Command-Line_Arguments|command-line arguments]], [[#Environment_Variables|environment variables]], [[#Property_Configuration_Files|configuration files]], etc. The property sources are described in detail in their [[#Property_Sources|corresponding sections]], below.
The Spring [[#Environment|environment]] is the only source of [[#Configuration_Property|configuration properties]] for components needing them. The environment abstracts the [[#Property_Sources|origins of properties]] and applies [[#Precedence|precedence rules]] when the same property is specified in more than one source. It pulls property values from several sources: [[#Java_System_Properties|Java system properties]], [[#Command-Line_Arguments|command-line arguments]], [[#Environment_Variables|environment variables]], [[#Property_Configuration_Files|configuration files]], etc. The property sources are described in detail in their [[#Property_Sources|corresponding sections]], below.

Revision as of 07:31, 2 December 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.

The Environment Abstraction

The environment is an abstraction integrated in the container that models two key aspects of the application environment: configuration properties and profiles.

The environment can be injected into a component and accessed programmatically as follows:

import org.springframework.core.env.Environment;
...
@Autowired
private Environment environment;

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 provides the user with a convenient service interface for configuring property sources and resolving properties from them.

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.

Reading Properties from Environment

The PropertySource Abstraction

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

Map Objects

Property Configuration Files

application.properties

application.properties

JNDI

Servlet Context Parameters

Configuration Service

The configuration service is also referred to as configuration server.

Precedence

Profiles

A profile is a named, logical group of bean definitions and configuration properties to be registered with the container only if the given profile is active. Beans may be assigned to a profile whether they are defined in XML or with annotations. The environment determines which profiles - if any - are currently active, and which profiles - if any - should be active by default.

Default Profile

Active Profile

Deplete This:

tmp