Spring Boot2.0之 yml的使用


yml

Spring Boot 默認讀取   .yml   .properties 結尾的

yml非常好的作用,比properties更節約  結構清晰

server:

  port:  8090

  context-path: /toov5

父級是公用的

一定要注意啊啊啊 空格! port:  8090 中間空格!

package com.toov5.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Index {
 
    @RequestMapping("/index")
    public String index(){
        return "ok";
    }
    
}

啟動類:

package com.toov5.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class app {
   public static void main(String[] args) {
    SpringApplication.run(app.class, args);
}
}

application.yml

server:
  port: 80
 

  啟動后:

修改成功!

訪問

 

注意在書寫時候,寫完: 一定要有空格  上下兩行空格兩個

 

 

 

是不是很好玩

 

復雜一點的無非就這這個的演變

server:
  port: 80
 
information:
  name: toov5
  age: 
    record: 666 
    

 

Java讀取操作:

package com.toov5.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Index {
    @Value("${information.name}")
    private String name;
    
    @Value("${information.age.record}")
    private String record;
    
    @RequestMapping("/index")
    public String index(){
        return "ok";
    }
    
    @RequestMapping("/getName")
    public String getInformation(){
        return name;
    }
    
    @RequestMapping("/getRecord")
    public String getRecord(){
        return record;
    }
      
}

 

 

 

 


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM