Spring Boot可以使用properties文件进行配置,并且也它为我们提供了application.properties配置文件,绝大部分均可使用application.properties配置文件进行配置,application.properties这个配置文件会被Spring Boot自动加载。也正是因为这个properties文件可以进行大部分的配置,有时该配置文件显得杂乱无章,不容易阅读。此外,实际开发中,我们也会选择自定义properties配置文件,但是,Spring Boot不能自动加载自定义的配置文件,需要我们实现自定义配置文件的加载,我们将通过一个例子介绍自定义properties配置文件。
创建新项目后,我们首先在pom.xml文件中添加Maven依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
接下里我们在resources文件夹下添加一个自定义properties配置文件db.properties,为了方便测试,添加一个student.name的属性,我们需要将student.name的值读取并显示在浏览器中,并将db.driverName的值在控制台输出:
db.driverName=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:@localhost:1521:orcl
db.username=test
db.password=test
student.name=Tom
在新建一个新的类DataResourceProp,代码如下:
@Configuration
public class DataResourceProp {
@Value("${db.driverName}")
private String driverName;
@Value("${db.url}")
private String url;
private String username;
@Value("${db.password}")
private String password;
// setters
public void setDriverName(String driverName) {
System.out.println(driverName);
this.driverName = driverName;
}
public void setUrl(String url) {
System.out.println(url);
this.url = url;
}
@Value("${db.username}")
public void setUsername(String username) {
System.out.println(username);
this.username = username;
}
public void setPassword(String password) {
System.out.println(password);
this.password = password;
}
// getters
public String getDriverName() {
return driverName;
}
public String getUrl() {
return url;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
在DataResourceProp中,我们利用@Value,并通${xxx}占位符读取配置文件中的内容。其中,@Value注解既可添加在属性上,也可以添加在方法上。
我们在创建一个控制器StudentController,在StudentController中,定义hello()方法,首先在控制台输出db.driverName的值,然后再给浏览器返回值, 代码如下:
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private DataResourceProp dataResourceProp;
@Value("${student.name}")
private String name;
@RequestMapping("/hello")
private String hello() {
System.out.println("DataResouceProp:" + dataResourceProp.getDriverName());
return "Hello " + name;
}
}
最后,我们创建Spring Boot启动类StartApplication,代码如下:
@PropertySource(value= {"classpath:db.properties"},ignoreResourceNotFound=false)
@SpringBootApplication(scanBasePackages= {"com.don"})
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
}
在Spring Boot启动类中利用@PropertySource注解指定properties文件路径,去类文件路径下查找指定的配置文件。value可以指定多个properties配置文件文件路径;布尔型参数ignoreResourceNotFound,默认值为false,表示没有找到指定路径的配置文件,会抛出FileNotFoundException异常,设置为true时,表示即使没有找到指定路径的配置文件,也不会报错。
我们启动项目,在浏览器中输入http://localhost:8080/student/hello,可看到浏览器中显示的内容如下,说明Spring Boot成功读取自定义配置文件中的student.name;查看控制台发现成功输出了属性db.driverName的值:
在DataResourceProp类中,我们还可以利用@ConfigurationProperties注解,但需要注意的是,使用@ConfigurationProperties注解需要指定ConfigurationProperties的prefix属性,因为该注解会将prefix属性的值与类的属性名称组合后,再去配置文件中查找对应的值,以此将配置文件中的属性值读取到类的属性中。使用了@ConfigurationProperties,我们就不用在类的每个属性或者set方法上一个一个的添加@Value注解,就可获取配置文件中的属性值了。
代码链接地址:https://pan.baidu.com/s/1hXTvW0wGIR4yXFAcAnbf9g
提取码:88k7