Springboot getting configuration information in the APPLICATION.YML

Source: Internet
Author: User

Hellocontroller.java

 PackageCom.springbootweb.demo.controller;Importcom.springbootweb.demo.entity.MyConfigProperties;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController; @RestController Public classHellocontroller {@Value ("${myconfig}")    PrivateString Myconfig; @Value ("${age}")    PrivateString age; @Value ("${content}")    PrivateString content; //@RequestMapping ("/hello")@RequestMapping (value={"/hello", "HI"})     Publicstring Say () {string str= "Hello Spring boot" "+myconfig+"; Age: "+Age ; STR+="===="; STR+=content; returnstr; } @AutowiredPrivatemyconfigproperties COF; @RequestMapping ("/conf")     Publicmyconfigproperties GetConfig () {returnCOF; }}

Myproperties.java

 Packagecom.springbootweb.demo.entity;Importorg.springframework.boot.context.properties.ConfigurationProperties;Importorg.springframework.stereotype.Component, @Component @configurationproperties (prefix= "MYCONFIG1") Public classmyconfigproperties {PrivateString name; PrivateInteger age;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicInteger getage () {returnAge ; }     Public voidsetage (Integer age) { This. Age =Age ; }}

DemoApplication

 Package Com.springbootweb.demo; Import org.springframework.boot.SpringApplication; Import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication  Public class demoapplication {    publicstaticvoid  main (string[] args) {        Springapplication.run (demoapplication. class , args);}    }

Application.yml

Server:   8081  Context-path:/"Myname:${myconfig},age:${age}"myconfig1:  Name: GYSSSSSDDDD  35

Springboot getting configuration information in the APPLICATION.YML

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.