Springboot Project Property Configuration

Source: Internet
Author: User

Springboot project, you can configure project-related information through the Application.properties configuration file.

Application.properties project configuration file, open is blank inside can be configured items, so configuration items we alt+/can prompt out

Previous Tomcat default port 8080 The default path is the root directory/

We now change to port 8888 and the context path/helloworld

Save after change, start Helloworldapplication class

Page Input: Http://localhost:8888/HelloWorld/hello

The results come out.

Springboot Support for custom attributes

We add a HelloWorld attribute to the Application.properties property value Spring Boot Hello

Of course, the Chinese byte encoding processing

Page we define and then configure a property value, call the request, you can return the configured value directly.

This is a very good function, more convenient;

We restart the Helloworldapplication class,

Page Input: Http://localhost:8888/HelloWorld/hello

Show:

If we want to configure multiple properties under a category,

such as MySQL's JDBC connection configuration

Mysql.jdbcname=com.mysql.jdbc.driver

Mysql.dburl=jdbc:mysql://localhost:3306/db_diary

Mysql.username=root

mysql.password=123456

We'll stick to application.properties.

Then according to the previous scheme, we write four attributes in the controller;

Reboot the Startup class,

Page Input: HTTP://LOCALHOST:8888/HELLOWORLD/SHOWJDBC

The above kind if a lot of attributes to write a large string if multiple places to use every place to write so much undesirable

Below we introduce Configurationproperties configuration method

Create a new Mysqlproperties class to configure all properties

Execute prefix MSYQL

Plus @component as a component for easy injection elsewhere

Of course, this will prompt

Need to introduce dependency to Pom.xml

We can just click.

Change it in the Helloworldcontroller.

It is much more convenient to define mysqlproperties, in fact, to encapsulate all the attributes into another object, to find the corresponding configuration value in the configuration file by prefix, and establish a mapping relationship.

Results:

Springboot Project Property 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.