Spring Boot uses @configurationproperties

Source: Internet
Author: User

Sometimes there is such a situation, we want to the configuration file information, read and automatically encapsulated into the entity class, this way, we use in the code is easier and more convenient,

At this point, we can use @configurationproperties, which can automatically encapsulate similar configuration information into entity classes.

First in the configuration file, this information is such a drop

Connection.username=Adminconnection.password=kyjufskifas2jsfsconnection.remoteaddress=  192.168. 1.1

There are two ways to use it:

1. Annotations on the class

To bind the data of the external configuration file properties to the attributes of the class

@Component @configurationproperties (prefix="Connection") Public classConnectionSettings {PrivateString username; PrivateString remoteaddress; PrivateString password;  PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString getremoteaddress () {returnremoteaddress; }     Public voidsetremoteaddress (String remoteaddress) { This. remoteaddress =remoteaddress; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }}

2. Bind the return object of the Bean method and the external configuration file

@Bean     " Connection " )    public  connectionsettings connectionsettings () {        returnNew  connectionsettings ();    }      Public Static void Main (string[] args) {        springapplication.run (demoapplication. class , args);    }

Spring Boot uses @configurationproperties

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.