Springboot reading configuration files and custom profiles

Source: Internet
Author: User

1. Create a MAVEN project to add dependencies to the Pom file

1<parent>2<groupId>org.springframework.boot</groupId>3<artifactId>spring-boot-starter-parent</artifactId>4<version>1.5.9.RELEASE</version>5</parent>6 7<dependencies>8<dependency>9<groupId>org.springframework.boot</groupId>Ten<artifactId>spring-boot-starter-web</artifactId> One</dependency> A<!--unit tests using- -<dependency> -<groupId>org.springframework.boot</groupId> the<artifactId>spring-boot-starter-test</artifactId> -</dependency> -    -<dependency> +<groupId>junit</groupId> -<artifactId>junit</artifactId> +<scope>test</scope> A</dependency> at     -</dependencies>

2. Create a project startup class Startapplication.java

1  PackageCom.kelly.controller;2 3 Importorg.springframework.boot.SpringApplication;4 Importorg.springframework.boot.autoconfigure.EnableAutoConfiguration;5 ImportOrg.springframework.context.annotation.ComponentScan;6 Importorg.springframework.context.annotation.Configuration;7 8 @Configuration9@EnableAutoConfiguration//automatically load configuration informationTen@ComponentScan ("com.kelly")//enables classes with annotations under the package path to use @autowired auto-injection One  Public classstartapplication { A      Public Static voidMain (string[] args) { -Springapplication.run (startapplication.class, args); -     } the}

3. Edit the configuration file application.properties and custom profile define.properties

  Application.properties

#访问的根路径server. Context-path=/springboot# port number server.port=8081#session失效时间server. session -timeout=30#编码server. Tomcat.uri-encoding=utf-8test.name=Kellytest.password= Admin123

 Define.properties

Definetest.pname=Testdefinetest.password=test123

4. Read the property values in the Application.properties configuration file

Firstcontroller.java

1  PackageCom.kelly.controller;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 9 @ControllerTen  Public classFirstcontroller { One      A@Value ("${test.name}") -     PrivateString name; -      the@Value ("${test.password}") -     PrivateString password; -      -@RequestMapping ("/") + @ResponseBody - String Home () +     { A         return"Hello springboot!"; at     } -      -@RequestMapping ("/hello") - @ResponseBody - String Hello () -     { in         return"Name:" + name + "," + "Password:" +password; -     } to}

5. Open the browser and enter Http://localhost:8081/springboot/hello to see the results

6. Read the custom configuration file using Java Bean define.properties

Defineentity.java

1  Packagecom.kelly.entity;2 3 Importorg.springframework.boot.context.properties.ConfigurationProperties;4 ImportOrg.springframework.context.annotation.PropertySource;5 Importorg.springframework.stereotype.Component;6 7 @Component8@ConfigurationProperties (prefix= "Definetest")9@PropertySource ("Classpath:define.properties")Ten  Public classdefineentity { One      A     PrivateString pname; -      -     PrivateString password; the  -      PublicString Getpname () { -         returnpname; -     } +  -      Public voidsetpname (String pname) { +          This. pname =pname; A     } at  -      PublicString GetPassword () { -         returnpassword; -     } -  -      Public voidSetPassword (String password) { in          This. Password =password; -     } to      +      -}

Secondcontroller.java

1  PackageCom.kelly.controller;2 3 Importorg.springframework.beans.factory.annotation.Autowired;4 ImportOrg.springframework.stereotype.Controller;5 Importorg.springframework.web.bind.annotation.RequestMapping;6 ImportOrg.springframework.web.bind.annotation.ResponseBody;7 8 Importcom.kelly.entity.DefineEntity;9 Ten @Controller One  Public classSecondcontroller { A      - @Autowired - defineentity defineentity; the      -@RequestMapping ("/define") - @ResponseBody - String define () +     { -         return"Test.name:" + defineentity.getpname () + ", Test.password:" +Defineentity.getpassword (); +     } A}

7. Open the browser, access the Http://localhost:8081/springboot/define, you can see the output results

Add: The directory structure of my project

If you encounter problems can also leave a message, if I see, whether it will be given and reply, we can discuss together, learn progress together.

Springboot reading configuration files and custom profiles

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.