欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Boot配置文件详解

程序员文章站 2022-07-15 08:06:59
...
一 新建项目config-test
二 编写pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>org.crazyit.cloud</groupId>
      <artifactId>config-test</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      
      <dependencies>
            <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
                  <version>1.5.7.RELEASE</version>
            </dependency>
            <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-devtools</artifactId>
                  <version>1.5.7.RELEASE</version>
            </dependency>           
      </dependencies>
      
</project>
三 编写启动类
package org.crazyit.cloud;

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

@SpringBootApplication
public class ConfigApp {

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

}
四 配置文件的4个路径
Spring Boot配置文件详解
访问优先级为1,2,3,4

五 编写控制类
package org.crazyit.cloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {
    
    @Autowired
    private ApplicationContext ctx;

    @GetMapping("/prop")
    @ResponseBody
    public String getName() {
        System.out.println("hello world");
        return ctx.getEnvironment().getProperty("test.user.name");
    }
} 
六 测试
Spring Boot配置文件详解
七 新建一个配置文件abc.properties
test.user.name = crazyit
八 新建MyconfigApp启动类
package org.crazyit.cloud;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class MyConfigApp {

    public static void main(String[] args) {
        new SpringApplicationBuilder(MyConfigApp.class).properties(
                "spring.config.location=classpath:/abc.properties").run(args);
    }
}
九 启动并测试
Spring Boot配置文件详解
十 新建yml配置文件
test:
  user:
    name: angus
十一 启动并测试
Spring Boot配置文件详解
十二 修改yml配置
spring:
  profiles: linux
test:
  user:
    name: angus
---
spring:
  profiles: windows
test:
  user:
    name: crazyit1
十三 新建一个启动类
package org.crazyit.cloud;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class ProfileTest {

    public static void main(String[] args) {
        new SpringApplicationBuilder(MyConfigApp.class).properties(
                "spring.profiles.active=windows", "server.port=9999").run(args);
    }

}
十四 测试 
Spring Boot配置文件详解
十五 Spring Boot内置属性