Spring Boot : 屬性配置&Controller註解補充(四)

目錄

1.屬性配置文件分類

屬性配置兩種文件: .properties和.yml,如我們想要配置tomcat啓動時端口和上下文,兩種文件配置分別如下:
properties :

server.port=8081
server.context-path=/boot

yml : (注意:後邊有空格) (推薦使用)

server:
  port: 8082
  context-path: /boot

大家配置完可以分別通過 http://localhost:8081/boot/http://localhost:8082/boot/ 測試下.

2.屬性配置在類中使用(@Value標籤的使用)

application.yml 如下,我們要使用配置文件中的name和age

server:
  port: 8081
  context-path: /
name: milo
age: 27

SampleController.java 如下 : 

package cn.milo.controllor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by admin on 2017/6/25.
 */
@Controller
public class SampleController {

    @Value("${name}")
    private String name;

    @Value("${age}")
    private Integer age;

    @RequestMapping("/")
    @ResponseBody
    public String demo1(){
        return "name : " + name + " , age = " + age;
    }
}

通過http://localhost:8081/驗證 輸出結果 :

name : milo , age = 27

有時我們需要在配置中使用上邊已經配置好的配置如 :

server:
  port: 8081
  context-path: /
name: milo
age: 27
shangmingtao: name is ${name} , age = ${age}

SampleController.java 如下 : 

    @Value("${shangmingtao}")
    private String shangmingtao;

    @RequestMapping("/")
    @ResponseBody
    public String demo1(){
        return shangmingtao;
    }

通過http://localhost:8081/驗證 輸出結果 :

name is milo , age = 27

3.屬性配置在類中使用(@ConfigurationProperties標籤的使用)

將配置文件修改爲如下 :

server:
 port: 8081
 context-path: /
shangmingtao:
 name: milo
 age: 27

新建一個java類, shangmingtao.java(這是我名字 :D)

package cn.milo.controllor;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Created by admin on 2017/7/4.
 */

@Component //這個註解不能丟,否則無法注入
@ConfigurationProperties(prefix = "shangmingtao") //指定前綴
public class shangmingtao {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

SampleController.java 如下 : 

package cn.milo.controllor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by admin on 2017/6/25.
 */
@Controller
public class SampleController {

    @Autowired
    private shangmingtao shangmingtao;

    @RequestMapping("/")
    @ResponseBody
    public String demo1(){
        return shangmingtao.getName() + " , " + shangmingtao.getAge();
    }
}

訪問 : http://localhost:8081/ 結果 :

milo , 27

4.spring.profiles.active 的使用

很多時候我們生產環境和開發環境用的配置是不一樣的.每次都要改一大堆配置很麻煩,所以我們通過spring.profiles.active進行切換. resources目錄結構 : 這裏寫圖片描述

application.yml

spring:
  profiles:
    active: dev #根據需要選擇啓動時選用的配置文件

application-dev.yml

server:
  port: 8081
  context-path: /
shangmingtao:
  name: milo
  age: 27

application-prod.yml

server:
  port: 8082
  context-path: /
shangmingtao:
  name: milo
  age: 18

通過spring-boot:run 或執行main方法啓動均可,通過訪問8081和8082驗證自己的配置是否正確.

5.jar 方式啓動

通過 clean package方式將項目打成jar包,jar包存放在target目錄中,不清楚的同學可以看下我之前寫的 maven入門:依賴 聚合 繼承(一) , 進到jar所在的目錄通過如下命令啓動 :

java -jar bootdemo4web.jar --spring.profiles.active=prod

和4中的測試結果一樣.

6.@RequestParam 和 @GetMapping 介紹

我之前寫過Controller常用註解的介紹, Spring Boot : 自動JSON轉換和熱部署(二) , 這裏做下補充.

@RequestParam 註解 : 此註解用來接通過url傳遞的參數,如: http://localhost:8082/?id=1 ,大家注意啊,這個url沒寫錯.@RequestMapping(“/”)時這樣寫是OK的.

看下代碼:

    @RequestMapping("/")
    @ResponseBody
    public String demo1(@RequestParam(value = "id" , defaultValue = "2") Integer id){
    /*
    defaultValue 中的值必須是字符串,RequestParam會自動轉型.
    */
        return shangmingtao.getName() + " , " + shangmingtao.getAge() + " , id = " +id;
    }

@GetMapping註解 :

@GetMapping("demo11") //和下方寫法等同
@RequestMapping(value = "demo11" , method = RequestMethod.GET)

還有@PostMapping同理.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章