Spring Boot Property Configuration and use

Source: Internet
Author: User
Tags dname
Spring Boot Property configuration and use

Spring Boot allows you to use the same application code in different environments via an external configuration, simply by injecting the properties into the configuration file or by modifying the default configuration. Spring Boot Series

Getting Started with Spring Boot

Spring Boot Property Configuration and use

Spring Boot Integration MyBatis

Spring Boot static resource processing

Spring boot-Configuration sort dependency Tips Spring boot supports multiple external configurations

These methods take precedence as follows: Command line arguments from java:comp/env Jndi properties Java System Properties (System.getproperties ()) Operating system environment variables Randomvaluepropertysource configured random.* property value jar Package external application-{profile}.properties or APPLICATION.YML (with Spring.profile Application-{profile}.properties or Application.yml (with spring.profile) configuration file inside the configuration file jar package Application.properties or Application.yml (without spring.profile) configuration file outside the jar package The application.properties or application.yml (without spring.profile) configuration file inside the jar bundle @Configuration the @propertysource on the annotation class Default Property command line arguments specified by springapplication.setdefaultproperties

Pass parameters by Java-jar App.jar--name= the "Spring"--server.port=9090 method.

Parameters are passed in the form of--xxx=xxx.

The parameters that can be used can be defined either by ourselves or by the default parameters in spring boot.

Many people may be concerned about how Web ports are configured such as the parameters provided in spring boot, and some of the available parameters are as follows:

# LOGGING
logging.path=/var/logs
logging.file=myapp.log
logging.config= # location of config file ( Default classpath:logback.xml for Logback)
logging.level.*= # levels for loggers, e.g. " Logging.level.org.springframework=debug "(TRACE, DEBUG, INFO, WARN, ERROR, FATAL, off)

# EMBEDDED SERVER CONFIGURATION (serverproperties)
server.port=8080
server.address= # bind to a specific NIC
Server.session-timeout= # Session Timeout in seconds
server.context-parameters.*= # Servlet Context init parameters, e.g. Server.context-parameters.a=alpha
server.context-path= # The context path, defaults to '/'
Server.servlet-path= # The servlet path, defaults to '/'
1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 The 12 13

More common application properties please browse here

Note: the command line arguments are behind the App.jar.

Command line configuration can be disabled by springapplication.setaddcommandlineproperties (false). Java System Properties

Note the Java System property location Java-dname= "isea533"-jar App.jar, the properties that can be configured are the same, with different precedence.

For example java-dname= "isea533"-jar app.jar--name= "spring!" The name value in is spring! operating system Environment variables

The configured java_home should know this one.

Here you need to note that some OS can not be used. This name, such as Server.port, can be configured using Server_port.

How to match the specific name, look at the back of this article. Randomvaluepropertysource

Where random numbers are used in the system, for example:

My.secret=${random.value}
My.number=${random.int}
My.bignumber=${random.long}
My.number.less.than.ten=${random.int (+)}
my.number.in.range=${random.int[1024,65536]}
1 2 3 4 5 1 2 3 4 5

Random.int* supports the value parameter and the Max parameter, and when the Max parameter is supplied, value is the minimum value. Apply a configuration file (. properties or. yml)

Write directly in the configuration file:

name=isea533
server.port=8080
1 2 1 2

The. yml format configuration files are as follows:

name:isea533
server:
    port:8080
1 2 3 1 2 3

In the case of a prefix, it is simpler to use the. yml format configuration file. About. yml configuration file Usage look here, please.

Note: when using. YML, there must be a space between the value of the property name and the colon, as name:isea533 is correct, and name:isea533 is wrong. location of the property configuration file

Spring looks for application.properties or application.yml from the/config directory or classpath root directory under Classpath.

/config takes precedence over the classpath root directory @PropertySource

This annotation can specify a specific property configuration file with a lower priority. springapplication.setdefaultproperties

For example:

Springapplication application = new Springapplication (application.class);
map<string, object> defaultmap = new hashmap<string, object> ();
Defaultmap.put ("name", "Isea-blog");
It can also be a Properties object
application.setdefaultproperties (defaultmap);
Application.Run (args);
1 2 3 4 5 6 1 2 3 4 5-6 apply (use) Properties @Value ("${xxx}")

This is the simplest way to inject attribute values through @value annotations. @ConfigurationProperties

Spring Boot makes it easy to inject attributes into a configuration object. For example:

my.name=isea533
my.port=8080
my.servers[0]=dev.bar.com
my.servers[1]=foo.bar.com
1 2 3 4 1 2 3 4

corresponding objects:

@ConfigurationProperties (prefix= "i") public
class Config {
    private String name;
    Private Integer port;
    Private list<string> Servers = new arraylist<string> ();

    Public String Gename () {return
        this.name;
    }

    Public Integer Geport () {return
        this.port;
    }
    Public list<string> Getservers () {return
        this.servers;
    }
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

Spring Boot automatically prefix= the "my" prefix to my properties.

Spring Boot automatically converts the type, and you need to be aware of initializing the list in the configuration when using the list.

Spring Boot also supports nested property injection, for example:

name=isea533
jdbc.username=root
jdbc.password=root
...
1 2 3 4 1 2 3 4

The corresponding configuration class:

@ConfigurationProperties public
class Config {
    private String name;
    private JDBC JDBC;
    Class Jdbc {
        private String username;
        private String password;
        Getter ...
    }

    Public Integer Geport () {return
        this.port;
    }
    Public Jdbc Getjdbc () {return
        this.jdbc
    }}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

The properties that begin with JDBC are injected into the JDBC object. using @configurationproperties on the @bean method

For example:

@ConfigurationProperties (prefix = "foo")
@Bean public
foocomponent foocomponent () {
    ...
}
1 2 3 4 5 1 2 3 4 5

Spring Boot injects the properties at the beginning of Foo into the Foocomponent object by name matching. Property Placeholder

For example:

App.name=myapp
App.description=${app.name} is a Spring Boot application
1 2 1 2

You can reference previously configured properties in the configuration file (available here before the priority level).

You can also set default values by using the ${app.name: Default name} method, and the default property is used when the referenced property is not found.

Because the ${} method will be processed by MAVEN. If you have inherited spring-boot-starter-parent,spring boot for Pom, you have changed the maven-resources-plugins default ${} to the @ @ method, such as @name@.

If you are introducing the spring Boot, you can modify the command parameters by using the other separator with the property placeholder

For example, modifying the Web default port requires using the--server.port=9090 method, if you write in the configuration:

server.port=${port:8080}
1 1

You can then use a shorter--port=9090 and use the default value of 8080 when the parameter is not supplied. property name matching rule

For example, you have the following configuration objects:

@Component
@ConfigurationProperties (prefix= "person") public
class ConnectionSettings {

    private String FirstName;

}
1 2 3 4 5 6 7 1 2 3 4 5 6-7

FirstName can be used as follows: Person.firstname, standard hump-style named Person.first-name, dashed (-) split method, recommended for use in. Properties and. Yml Configuration Files Person_ first_name, uppercase underline, recommended to use attribute validation in system environment variables

You can use the JSR-303 annotation for validation, for example:

@Component
@ConfigurationProperties (prefix= "Connection") public
class ConnectionSettings {

    @NotNull
    private inetaddress remoteaddress;

    ... getters and setters

}
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9-10 last

These are the contents of the spring boot attribute configuration and use, some of which are not comprehensive or have more questions for the reader to view the spring boot complete document or externalized Configuration.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.