Spring读取配置文件的方法

来源:互联网 发布:机器人姿态矩阵 编辑:程序博客网 时间:2024/05/29 03:18

一) 读取默认的application.properties或application.yml

1. 使用@Value

 @Value("${sql.name}") private String sqlName;
2. 使用Environment

 @Autowired private Environment env
 String sqlName = env.getProperty("sql.name");

二)使用@ConfigurationProperties读取自定义配置文件
@ConfigurationProperties(locations = "classpath:config/my-web.properties", prefix = "sql")@Componentpublic class MyWebConfig {    private String name;    public String getName() {        return name;    }    public String setName(String name){        this.name = name;    } }
调用:

@Autowiredprivate MyWebConfig config;String sqlName = config.getName();


0 0
原创粉丝点击