Spring-boot Notes-attribute configuration (ii)

Source: Internet
Author: User
Tags custom name server port

This article mainly introduces the property configuration and content of Springboot, and the use of custom attributes. First, attribute configuration 1.1 External Command configuration

Pass parameters by Java-jar Xx.jar--name= the "Spring"--server.port=9090 method. "ServerPort Front is two hillock"
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.
Note: The command line arguments are behind the Xxx.jar.
Command line configuration can be disabled by springapplication.setaddcommandlineproperties (false). 1.2 configuration file Configuration

Configurations such as port numbers, contexts, and so on in the Web project that we are more concerned with, can be configured in the application configuration file (. properties or. yml):

# Configure database
spring.jpa.database = PostgreSQL
# query Displays log
Spring.jpa.show-sql = True
# Hibernate DDL Auto ( Create, Create-drop, update)
Spring.jpa.hibernate.ddl-auto = Update
# stripped before adding them to the entity Ma Nager)
spring.jpa.properties.hibernate.dialect = Org.hibernate.dialect.PostgreSQLDialect

server.port= 8080
#server. address= # bind to a specific NIC
#server. session-timeout= # Sessions Timeout in seconds
#server . context-parameters.*= # Servlet Context init parameters, e.g. Server.context-parameters.a=alpha
# The context path , defaults to '/'
server.context-path=/demo
#server. servlet-path= # The servlet path, defaults to '/'

In fact, we can configure a lot of parameters, specific can refer to parameter configuration II, custom properties using the configuration in application.properties

# #自定义属性
#32位随机字符串
gh.rd-secret=${random.value}
#int类型的随机数字
gh.rd-intvalue=${random.int[ 1024,65536]}
#自定义名字
gh.name=www.loveaocai.com
#属性占位符属性
gh.desc=${gh.name} is a domain name
Way One: Direct access
@Value (Value = "${gh.rd-secret}")
    private String randomvalue;

    The random numeric @Value of type int
    (Value = "${gh.rd-intvalue}")
    private int randomint;

    Custom name
    @Value (Value = "${gh.name}")
    private String name;

    Property Placeholder Property ${jerome.name} is a domain name
    @Value (Value = "${gh.desc}")
    private String desc;

    @RequestMapping ("Testprop") public
    map<string, object> testprop () {
        map<string, object> Map = New Hashmap<> ();
        Map.put ("title", "Hello World");
        Map.put ("Randomvalue", randomvalue);
        Map.put ("Randomint", randomint);
        Map.put ("name", name);
        Map.put ("desc", desc);
        return map;
    }

by @value (value = "${gh.name}") annotations can directly obtain the value of the properties file, directly used in the method. However, this approach seems to have been deprecated, but it can also be used. Mode Two: Configure the attribute class

@ConfigurationProperties (prefix= "GH") public
class Ghproperties {
    private String name;
    Private String desc;
    Private String Rdsecret;
    Private Integer Rdintvalue;
    Getter Setter 
 }

and add the configuration in the initialized class:

The following sentence
@EnableConfigurationProperties ({ghproperties.class})
@SpringBootApplication public
class Application {public
    static void Main (string[] args) {
        springapplication.run (application.class, args);
    }
}

The use of the following methods:

@Autowired
    private ghproperties ghproperties;

    @RequestMapping ("TESTPROP2") public
    map<string, object> testProp2 () {
        map<string, object> Map = New Hashmap<> ();
        Map.put ("title", "Hello World22");
        Map.put ("Randomvalue", Ghproperties.getrdsecret ());
        Map.put ("Randomint", Ghproperties.getrdintvalue ());
        Map.put ("Name", Ghproperties.getname ());
        Map.put ("desc", Ghproperties.getdesc ());
        return map;
    }
Effect chart

three, multi-environment configuration

When we develop the spring boot application, the same set of programs is often applied and installed in several different environments, such as development, testing, production, and so on. The database address, server port, and so on in each environment will be different, and it would be tedious and error-prone to modify the configuration files frequently when packaging for different environments.

For a multiple-environment configuration, the basic idea of a variety of project-building tools or frameworks is consistent, and the Spring boot is no exception, or simpler, by configuring multiple configurations of different environments, and then packaging the commands to specify what needs to be packaged.

In spring boot, the multi-environment profile name needs to satisfy the application-{profile}.properties format, where {profile} corresponds to your environment identity, such as:

Application-dev.properties: Development environment application-test.properties: test environment

Application-prod.properties: Production environment

As to which specific configuration file will be loaded, it needs to be set in the Application.properties file by the Spring.profiles.active property, whose value corresponds to the {profile} value.

such as: Spring.profiles.active=test will load application-test.properties configuration file content

Here for the time being only introduced properties of the way, as for the YAML mode is not discussed.

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.