1. 程式人生 > >SpringBoot初體驗(yml的使用、注入的兩種方法、多環境配置、原理Demo)

SpringBoot初體驗(yml的使用、注入的兩種方法、多環境配置、原理Demo)

yml的使用

yml的格式有兩種:
注意!!key和value之間的:有空格,必須要有空格!!
物件格式

student:
  name : "吳師傅"
  age : 18

普通格式

student.name : "吳師傅"
student.age : 18

yml使用的Demo(為了方便檔案全部都放在Java資料夾下與application.java同級)
實體類

package com.bdqn.useyml;

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

//component:加入到Bean容器內
@Component
//prefix的值對應application.yml內的名字,這個類的屬性對應其屬性
@ConfigurationProperties(prefix = "student")
public class Students {
    //對應yml裡面的屬性
    private String name;
    private int age;
    //必須set get
    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

application.yml

student:
  name : "吳師傅"
  age : 18

Controller

package com.bdqn.useyml;

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

@RestController
public class Mycontroller {

    @Autowired
    private Students student;

    @RequestMapping("/")
    public String hello(){
        return student.getName()+student.getAge();
    }

}

修改起始讀取檔案(resources資料夾下的application)
在入口檔案修改

package com.bdqn.useyml;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;

//追加讀取配置檔案(系統預設為resources資料夾下XXXapplication),若resources裡面有application,也會讀取
@PropertySource("classpath:/config/config.yml")
@SpringBootApplication
public class UseymlApplication {

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

config.yml

server.port : 8023

@value與@configurationpProperties的使用

雖然兩個都是Springboot讀值,但比較常用給的是@configurationpProperties,前面的例子已經使用過@configurationpProperties了,所以這裡只介紹@value的使用demo

實體類

package com.bdqn.useyml;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//component:加入到Bean容器內
@Component
public class Students {
    //value的使用
    @Value("${student.name}")
    private String name;
    @Value("${student.age}")
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

application.yml

student:
  name : "吳師傅"
  age : 18

Controller還是跟上面的一樣

package com.bdqn.useyml;

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

@RestController
public class Mycontroller {

    @Autowired
    private Students student;

    @RequestMapping("/")
    public String hello(){
        return student.getName()+student.getAge();
    }

}

Springboot獲取容器方法

MyUtil.java

package com.bdqn.useyml;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
//加入到bean容器內
@Component
//獲取applicationContextAware
public class MyUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //類似以前Struts2獲取session那樣,此方法需要必須重寫
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (MyUtil.applicationContext == null) {
            MyUtil.applicationContext = applicationContext;
            System.out.println("走構造了");
        }
    }

    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

}

實體類

package com.bdqn.useyml;

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

//component:加入到Bean容器內,同時也是這個bean的name和id
@Component("student")
@ConfigurationProperties(prefix = "student")
public class Students {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

在text資料夾下的檔案測試獲取bean

package com.bdqn.useyml;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UseymlApplicationTests {

    @Test
    public void contextLoads() {
        //bean的名字與component的value對應,若component沒有設定value則會預設類名,類名首字母小寫
        Students students=(Students) MyUtil.getBean("student");
        System.out.println(students.getName());
    }

}

application.yml

student:
  name : "吳師傅"
  age : 18

多環境配置

properties版
三個檔案 主:application.properties(負責選擇是生產還是發開)   子-開發環境:application-dev.properties  子-生產環境:application-pro.properties
application.properties檔案

spring.profiles.active=pro

application-dev.properties檔案(開發環境,這裡只是改埠而已,後面可以詳細分功能)

server.port = 8081

application-pro.properties檔案(生產環境)

server.port = 8082

yml配置

student:
  name : "吳師傅"
  age : 18
---
spring:
 profiles:
  active : dev
---
spring:
 profiles: pro
server:
 port: 8082
---
spring:
 profiles: dev
server:
 port: 8081