Spring Boot Project Property configuration

Source: Internet
Author: User

Next to the Getting Started tutorial, let's learn about the project property configuration for spring boot.

1. Configure project built-in properties

The property configuration is primarily configured in the Application.properties file (automatically prompted when writing) here we change the server's port to 8888, the path plus the HelloWorld:

Click the Run button on the Deomapplication.java page to open the browser input: Http://localhost:8888/HelloWorld/hello

At this point, the output information of the console can also see that the port becomes 8888:

The previous URL is invalid:

The changed URL is valid:

2. Configure custom properties

It is also written in the Application.properties file, which reads as follows:

Then use the @value annotation in Helloworldcontroller.java to inject the custom attribute, so that you can use the custom attribute directly:

Run the project, enter: Http://localhost:8888/HelloWorld/hello, the browser will display the following results:

3. Configurationproperties Configuration

Create a new com.example.properties package, and then create a new Mysqlproperties.java class, the test run will be error, unable to find the bean, the error is as follows:

Baidu after found the reason:

The Bean assembly default rules for Springboot projects are scanned from top to bottom based on the package location where the application class resides!

if the package containing the application class is: io.github.gefangshuai.app , only the package io.github.gefangshuai.app and all its child packages will be scanned, and if the service or DAO is in a package that is not io.github.gefangshuai.app under its child package, it will not be scanned!
So the Mysqlproperties.java class moved under the Com.example.demo package, that is, the application class is located in the package, there is no report bean can not find the error.

The Mysqlproperties.java uses 2 important annotations:

1,@Component: The ordinary pojo into the spring container, equivalent to the <bean in the configuration file id= "" Class= "/>

2.@ConfigurationProperties(prefix = "mysql"): Inject the configuration class into the spring container so that you can use the configuration class, where the prefix is used.

When writing is complete, idea will prompt for a problem with the attribute because the Spring-boot-configuration-processor.jar package is not introduced and the following dependencies are added in Pom.xml:

1 <Dependency>2     <groupId>Org.springframework.boot</groupId>3     <Artifactid>Spring-boot-configuration-processor</Artifactid>4     <Optional>True</Optional>5 </Dependency>

The Mysqlproperties.java code is as follows (using the Alt+insert shortcut to generate getter and setter methods):
1  PackageCom.example.demo;2 3 Importorg.springframework.boot.context.properties.ConfigurationProperties;4 Importorg.springframework.stereotype.Component;5 6 /**7 * MySQL attribute configuration8  */9 @ComponentTen@ConfigurationProperties (prefix = "MySQL") One  Public classmysqlproperties { A  -     PrivateString Jdbcname; -  the     PrivateString Dburl; -  -     PrivateString UserName; -  +     PrivateString password; -  +      PublicString Getjdbcname () { A         returnJdbcname; at     } -  -      Public voidsetjdbcname (String jdbcname) { -          This. Jdbcname =Jdbcname; -     } -  in      PublicString Getdburl () { -         returnDburl; to     } +  -      Public voidSetdburl (String dburl) { the          This. Dburl =Dburl; *     } $ Panax Notoginseng      PublicString GetUserName () { -         returnUserName; the     } +  A      Public voidsetusername (String userName) { the          This. UserName =UserName; +     } -  $      PublicString GetPassword () { $         returnpassword; -     } -  the      Public voidSetPassword (String password) { -          This. Password =password;Wuyi     } the}

Application.properties Add the following properties:

1 Mysql.jdbcname=com.mysql.jdbc.driver 2 Mysql.dburl=jdbc:mysql://localhost:3306/db_boot 3 Mysql.username=root 4 Mysql.password=root

The final code for Hellowordcontroller.java is:

1  PackageCom.example.demo;2 3 ImportOrg.springframework.beans.factory.annotation.Value;4 ImportOrg.springframework.stereotype.Controller;5 Importorg.springframework.web.bind.annotation.RequestMapping;6 ImportOrg.springframework.web.bind.annotation.ResponseBody;7 8 ImportJavax.annotation.Resource;9 Ten  One /** A * Created on 2017-9-3. -  */ - @Controller the  Public classHelloworldcontroller { -  -@Value ("${hellworld}") -     PrivateString HelloWorld; +  - @Resource +     Privatemysqlproperties Mysqlpropertie; A  at@RequestMapping ("/hello") - @ResponseBody -      PublicString Say () { -         returnHelloWorld; -     } -  in@RequestMapping ("/showjdbc") - @ResponseBody to      PublicString Showjdbc () { +         return"Mysql.jdbcname" + mysqlpropertie.getjdbcname () + "<br/>" -+ "Mysql.dburl" + mysqlpropertie.getdburl () + "<br/>" the+ "Mysql.username" + mysqlpropertie.getusername () + "<br/>" *+ "Mysql.password" + mysqlpropertie.getpassword () + "<br/>"; $ Panax Notoginseng     } -  the  +}

Run the project, browser input: HTTP://LOCALHOST:8888/SHOWJDBC, the correct result is as follows:

Reprint please attach the original link: http://www.cnblogs.com/stm32stm32/p/7469663.html

Spring Boot 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.