Spring boot 属性配置文件

先看下整个项目的目录
这里写图片描述

一、多环境配置
由于我们开发项目,一般会有测试环境、开发环境和生产环境,不同的环境我们的数据库地址、端口等都不同。所以我们要有对应的配置文件。如下:

1、新建一个Spring Starter Project项目,只选择web依赖
这里写图片描述

2、我们新建3个配置文件,注意路径要在resources目录下
这里写图片描述

这里写图片描述

application-dev.properties:开发环境
application-prod.properties:生产环境
application-test.properties:测试环境

3、我们在3个配置文件中只简单的设置端口号
(1)开发环境application-dev.properties:

#set development environment server port
server.port=1111

(2)application-prod.properties:生产环境

#set product environment server port
server.port=2222

(3)application-test.properties:测试环境

# set test environment server port 
server.port=3333

4、我们写个测试的Testcontroller,里面的代码很简单

package com.yuna.demo;

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

@RestController
public class TestController {

    @RequestMapping("/test")
    public String test() {
        return "this is test view";
    }
}

5、我们在application.properties配置文件中,设置下我们的环境,我们选择的是dev,开发环境

#set current run environment  
spring.profiles.active=dev
# this is product environment
#spring.profiles.active=prod

# this is test environment
#spring.profiles.active=test

6、我们运行下,run as spring boot app
然后我们在浏览器中输入http://localhost:1111/test

注意:在默认情况下,我们的.properties文件中是不能输入汉字的,我们需要如下设置,选中配置文件右击—properties—resources,Text file encoding,选择utf-8
这里写图片描述

二、加载自定义的属性

看下项目目录
这里写图片描述
1、我们在配置文件application.properties中设置自定义属性

#--------------自定义属性------------------------
com.yuna.show.name = 夏沫
com.yuna.show.title = 学习spring boot
com.yuna.show.desc = ${com.yuna.show.name}在认真${com.yuna.show.title}
com.yuna.show.value = ${random.value}

2、我们新建一个ShowProperties.java,然后通过@Value(“${属性名}”)注解来加载对应的配置属性

package com.yuna.service;


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

@Component
public class ShowProperties {


    @Value("${com.yuna.show.name}")
    private String name;

    @Value("${com.yuna.show.title}")
    private String title;

    @Value("${com.yuna.show.desc}")
     private String desc;

    @Value("${com.yuna.show.value}")
     private String value;

    public String getName() {
        return name;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

3、我们在TestController.java中写个方法测试下

@Autowired
    private ShowProperties showProperties;

@RequestMapping("/show")
    public String show() {
        String str  =  showProperties.getTitle();
        return str;
    }

4、等运行后报如下问题

这里写图片描述

我们需要在Application.java中添加注解,因为我们的ShowProperties没有扫描到

package com.yuna.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
//解决方法一:
//@SpringBootApplication
//@ComponentScan(basePackages = {"com.yuna.demo","com.yuna.service"})

//解决方法二:
@SpringBootApplication(scanBasePackages= {"com.yuna"})
public class Application {

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

5、启动服务,然后在浏览器中输入http://localhost:1111/show
发现可以显示,但是是乱码,所以我们在取配置文件中的值时,需要转下码

@RequestMapping("/show")
    public String show() {

        String str  =  showProperties.getTitle();
        String string;
        try {
            string = new String(str.getBytes("ISO-8859-1"), "utf-8");//需要转码,不然有的会显示乱码
            System.out.println("showProperties--string-->" + string);
            return string;
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "";
    }

至此就OK了~

猜你喜欢

转载自blog.csdn.net/ycf03211230/article/details/78920635