读取自定义属性文件

  • 新建属性文件jdbc.properties

database.driverName=com.mysql.jdbc.Driver
database.url=jdbc.mysql://localhost:3306/chapter3
database.username=root
database.password=123456
  • 使用@PropertySource注解

package com.springboot.chapter3;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;

@SpringBootApplication(scanBasePackages = {"com.springboot.chapter3"})
@PropertySource(value = {"classpath:jdbc.properties"},ignoreResourceNotFound = true)
public class Chapter3Application {

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

}

  • 注意点

1.需要设置环境变量CLASSPATH

.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar

2.value可以配置多个配置文件。使用classpath前缀,意味着去类文件路径下找到属性文件;ignoreResourceNotFound代表是否忽略配置文件找不到的问题。ignoreResourceNotFound的默认值为false,也就是没有找到属性文件,就会报错;配置为true,表示找不到就忽略掉,不会报错。

posted @ 2020-04-11 21:01  xl4ng  阅读(378)  评论(0)    收藏  举报