第二章 SpringBoot-配置文件

前言

在第一章我们简单的学习了什么是Springboot以及它的特点,并通过一个helloworld案例来了解它,接下来,我们来了解一下springboot的配置文件生效方式。

1. 创建Maven项目,导入需要的依赖

Maven目录结构如下:

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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>springboot</groupId>
    <artifactId>2_springboot_properties</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Spring Boot 启动父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <dependencies>
        <!-- Spring Boot Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot Test 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>1.4.2.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

2. 创建application-dev.properties开发环境

## dev 开发环境
userq.name=惘
userq.age=20
userq.desc=dev: ${userq.name} : ${userq.age}

3. 创建application-prod.properties生产环境

## prop 生成环境
userq.name=顾
userq.age=19
userq.desc=prop: ${userq.name} : ${userq.age}

4. 创建application.properties配置文件

# Spring Profiles Active
spring.profiles.active=prop

5. 创建User.java

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

@Component
@ConfigurationProperties(prefix="userq")
public class UserProperties {

    private String name;
    private Integer age;
    private String desc;
    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;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
    @Override
    public String toString() {
        return "UserProperties [name=" + name + ", age=" + age + ", desc=" + desc + "]";
    }


}

6. 创建Test.java

import java.io.UnsupportedEncodingException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.wzw.springboot.properties.UserProperties;

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

    private static final Logger LOGGER = LoggerFactory.getLogger(UserPropertiesTest.class);

    @Autowired
    private UserProperties userProperties;

    @Test
    public void fun1() throws UnsupportedEncodingException {
        LOGGER.info("" + new String(userProperties.toString().getBytes("iso8859-1"), "utf-8"));
    }
}

7.运行fun1()

控制台打印
UserProperties [name=顾, age=19, desc=prop: 顾 : 19]

注意:如果在application-dev.properties或者application-prop.properties中

user.name=惘
user.age=19
user.desc=prop: ${user.name} : ${user.age}

最后测试的结果会是
UserProperties [name=Administrator, age=20, desc=dev: Administrator : 20]
这是因为user.name会自动找电脑的用户名。

8. .yml文件

  1. YAML 是专门用来写配置文件的语言,非常简洁和强大,远比 JSON 格式方便。
  2. YAML易于人们阅读。
  3. YAML数据在编程语言之间是可移植的。
  4. YAML具有表现力和可扩展性。
  5. YAML易于实现和使用。
  6. 使用空格 Space 缩进表示分层,不同层次之间的缩进可以使用不同的空格数目,但是同层元素一定左对齐,即前面空格数目相同(不能使用 Tab,各个系统 Tab对应的 Space 数目可能不同,导致层次混乱)
  7. ‘#’表示注释,只能单行注释,从#开始处到行尾

9. 创建application.yml文件

## 书信息
book:
  name: 《SpringBoot 入门》
  writer: 惘

10. 创建Book实体类

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

@Component
@ConfigurationProperties(prefix = "book")
public class BookComponent {

    private String name;

    private String writer;

    public String getName() {
        return name;
    }

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

    public String getWriter() {
        return writer;
    }

    public void setWriter(String writer) {
        this.writer = writer;
    }

    @Override
    public String toString() {
        return "BookComponent [name=" + name + ", writer=" + writer + "]";
    }

}

11.编写测试类,运行fun1()

import java.io.UnsupportedEncodingException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.wzw.springboot.properties.BookComponent;

@RunWith(SpringRunner.class)
@SpringBootTest
public class BookPropertiesTest {
private static final Logger LOGGER = LoggerFactory.getLogger(UserPropertiesTest.class);

    @Autowired
    private BookComponent bookComponent;

    @Test
    public void fun1() throws UnsupportedEncodingException {
        LOGGER.info("====================" + bookComponent.toString());
    }
}

运行结果:
====================BookComponent [name=《SpringBoot 入门》, writer=惘]

猜你喜欢

转载自blog.csdn.net/qq_34229789/article/details/79939613