SpringBoot2.0中使用自定义properties文件

一.在resources目录下添加自定义的test.properties文件

test.properties内容如下:

host=127.0.0.1
port=8080

二.编写一个读取配置文件内容的类

@Configuration
@PropertySource("classpath:test.properties")//注意路径
public class TestConfig {

    @Value("${host}")
    private String host;
    
    @Value("${port}")
    private int port;

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }
}

三.配置文件获取

@Autowired
private TestConfig testConfig;

public void test() {
    System.out.peintln(testConfig.getHost())
}

猜你喜欢

转载自www.cnblogs.com/JoeyWong/p/9072219.html