1.先引入maven依赖
org.springframework.boot
spring-boot-configuration-processor
true
2.新建一个Java类
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* 常量类
*
*/
@Configuration
@ConfigurationProperties(prefix = "constant")
@PropertySource("classpath:constant.properties")
public class Constant {
private String name;
private String sex;
//省略get set toString
}
prefix代表属性文件中的前缀,@PropertySource指定加载的那个属性文件,如是默认的application.properties 则不用指定。
//加载多个文件
@PropertySource({
"classpath:config.properties",
"classpath:db.properties