Spring Boot加载Properties文件的方法

最近使用Spring Boot框架写了一个小网站.感觉Spring Boot写网站十分的优雅

本文将介绍如何在Spring Boot内引用Properties的值

1.开启文件扫描

2.在需要引入配置文件的Class上使用@PropertySource注解.

3.在Class中的属性上使用@Value注解来注入配置文件中的值.

 

演示

package org.jzbk.example.util;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Getter
@Component
@PropertySource(value = "classpath:versionInfo.properties")
public class VersionInfo {
    @Value("${example.platform}")
    private String platform;

    @Value("${example.version}")
    private String version;

    @Value("${example.branch}")
    private String branch;

    @Value("${example.buildDate}")
    private String buildDate;
}

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注


此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据