最近微框架spring-boot很火,笔者也跟风学习了一下,废话不多说,现给出一个读取配置文件的例子。
首先,需要在pom文件中依赖以下jar包
其次,我们需要一个spring-boot启动类
1.读取properties配置文件
在类路径下放置一个application.properties文件,大致内容如下:
首先,需要在pom文件中依赖以下jar包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
其次,我们需要一个spring-boot启动类
@SpringBootApplication
@EnableConfigurationProperties({PropsConfig.class,YmlConfig.class})
public class ReadApplication {
public static void main(String[] args) {
SpringApplication.run(ReadApplication.class, args);
}
}
没错,@EnableConfigurationProperties注解里指出的PropsConfig.class,YmlConfig.class分别就是读取props和yml配置文件的类。接下来,我们分别进行读取properties和yml配置文件的具体实现。
1.读取properties配置文件
在类路径下放置一个application.properties文件,大致内容如下:
master.ds.driverClassName=com.mysql.jdbc.Driver
master.ds.url=jdbc:mysql://localhost:3306/test
master.ds.username=root
master.ds.password=root
master.ds.filters=stat
master.ds.maxActive=20
master.ds.initialSize=1
master.ds.maxWait=60000
master.ds.minIdle=10
master.ds.timeBetweenEvictionRunsMillis=60000
master.ds.minEvictableIdleTimeMillis=300000
master.ds.validationQuery=SELE