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