Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
d5441d27
Commit
d5441d27
authored
Jul 19, 2016
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish doc
parent
a2d8a769
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
38 deletions
+54
-38
spring-boot-features.adoc
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
+54
-38
No files found.
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
View file @
d5441d27
...
@@ -780,9 +780,8 @@ the configuration of your application. For example:
...
@@ -780,9 +780,8 @@ the configuration of your application. For example:
[source,java,indent=0]
[source,java,indent=0]
----
----
@Component
@ConfigurationProperties(prefix="connection")
@ConfigurationProperties(prefix="connection")
public class Connection
Setting
s {
public class Connection
Propertie
s {
private String username;
private String username;
...
@@ -806,9 +805,49 @@ String. Some people use Project Lombok to add getters and setters automatically.
...
@@ -806,9 +805,49 @@ String. Some people use Project Lombok to add getters and setters automatically.
TIP: See also the <<boot-features-external-config-vs-value,differences between `@Value`
TIP: See also the <<boot-features-external-config-vs-value,differences between `@Value`
and `@ConfigurationProperties`>>.
and `@ConfigurationProperties`>>.
The `@EnableConfigurationProperties` annotation is automatically applied to your project
You also need to list the properties classes to register in the
so that any beans annotated with `@ConfigurationProperties` will be configured from the
`@EnableConfigurationProperties` annotation:
`Environment` properties. This style of configuration works particularly well with the
[source,java,indent=0]
----
@Configuration
@EnableConfigurationProperties(ConnectionProperties.class)
public class MyConfiguration {
}
----
[NOTE]
====
When `@ConfigurationProperties` bean is registered that way, the bean will have a
conventional name: `<prefix>-<fqn>`, where `<prefix>` is the environment key prefix
specified in the `@ConfigurationProperties` annotation and <fqn> the fully qualified
name of the bean. If the annotation does not provide any prefix, only the fully qualified
name of the bean is used.
The bean name in the example above will be `connection-com.example.ConnectionProperties`,
assuming that `ConnectionProperties` sits in the `com.example` package.
====
Even if the configuration above will create a regular bean for `ConnectionProperties`, we
recommend that `@ConfigurationProperties` only deal with the environment and in particular
does not inject other beans from the context. Having said that, The
`@EnableConfigurationProperties` annotation is _also_ automatically applied to your project
so that any _existing_ bean annotated with `@ConfigurationProperties` will be configured
from the `Environment` properties. You could shortcut `MyConfiguration` above by making
sure `ConnectionProperties` is a already a bean:
[source,java,indent=0]
----
@Component
@ConfigurationProperties(prefix="connection")
public class ConnectionProperties {
// ... getters and setters
}
----
This style of configuration works particularly well with the
`SpringApplication` external YAML configuration:
`SpringApplication` external YAML configuration:
[source,yaml,indent=0]
[source,yaml,indent=0]
...
@@ -830,8 +869,12 @@ as any other bean.
...
@@ -830,8 +869,12 @@ as any other bean.
@Service
@Service
public class MyService {
public class MyService {
private final ConnectionProperties connection;
@Autowired
@Autowired
private ConnectionSettings connection;
public MyService(ConnectionProperties connection) {
this.connection = connection;
}
//...
//...
...
@@ -844,30 +887,6 @@ as any other bean.
...
@@ -844,30 +887,6 @@ as any other bean.
}
}
----
----
It is also possible to shortcut the registration of `@ConfigurationProperties` bean
definitions by simply listing the properties classes directly in the
`@EnableConfigurationProperties` annotation:
[source,java,indent=0]
----
@Configuration
@EnableConfigurationProperties(ConnectionSettings.class)
public class MyConfiguration {
}
----
[NOTE]
====
When `@ConfigurationProperties` bean is registered that way, the bean will have a
conventional name: `<prefix>-<fqn>`, where `<prefix>` is the environment key prefix
specified in the `@ConfigurationProperties` annotation and <fqn> the fully qualified
name of the bean. If the annotation does not provide any prefix, only the fully qualified
name of the bean is used.
The bean name in the example above will be `connection-com.example.ConnectionSettings`,
assuming that `ConnectionSettings` sits in the `com.example` package.
====
TIP: Using `@ConfigurationProperties` also allows you to generate meta-data files that can
TIP: Using `@ConfigurationProperties` also allows you to generate meta-data files that can
be used by IDEs. See the <<configuration-metadata>> appendix for details.
be used by IDEs. See the <<configuration-metadata>> appendix for details.
...
@@ -892,7 +911,7 @@ its bean registration:
...
@@ -892,7 +911,7 @@ its bean registration:
----
----
Any property defined with the `foo` prefix will be mapped onto that `FooComponent` bean
Any property defined with the `foo` prefix will be mapped onto that `FooComponent` bean
in a similar manner as the `Connection
Setting
s` example above.
in a similar manner as the `Connection
Propertie
s` example above.
...
@@ -908,9 +927,8 @@ For example, given the following `@ConfigurationProperties` class:
...
@@ -908,9 +927,8 @@ For example, given the following `@ConfigurationProperties` class:
[source,java,indent=0]
[source,java,indent=0]
----
----
@Component
@ConfigurationProperties(prefix="person")
@ConfigurationProperties(prefix="person")
public class
ConnectionSetting
s {
public class
OwnerPropertie
s {
private String firstName;
private String firstName;
...
@@ -971,9 +989,8 @@ annotations to your `@ConfigurationProperties` class:
...
@@ -971,9 +989,8 @@ annotations to your `@ConfigurationProperties` class:
[source,java,indent=0]
[source,java,indent=0]
----
----
@Component
@ConfigurationProperties(prefix="connection")
@ConfigurationProperties(prefix="connection")
public class Connection
Setting
s {
public class Connection
Propertie
s {
@NotNull
@NotNull
private InetAddress remoteAddress;
private InetAddress remoteAddress;
...
@@ -985,13 +1002,12 @@ annotations to your `@ConfigurationProperties` class:
...
@@ -985,13 +1002,12 @@ annotations to your `@ConfigurationProperties` class:
In order to validate values of nested properties, you must annotate the associated field
In order to validate values of nested properties, you must annotate the associated field
as `@Valid` to trigger its validation. For example, building upon the above
as `@Valid` to trigger its validation. For example, building upon the above
`Connection
Setting
s` example:
`Connection
Propertie
s` example:
[source,java,indent=0]
[source,java,indent=0]
----
----
@Component
@ConfigurationProperties(prefix="connection")
@ConfigurationProperties(prefix="connection")
public class Connection
Setting
s {
public class Connection
Propertie
s {
@NotNull
@NotNull
@Valid
@Valid
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment