spring.datasource.url:jdbc:mysql:127.0.0.1:3306/
spring.datasource.username:root
spring.datasource.password:123456
spring.datasource.driver-class-name:com.mysql.jdbc
数据库配置目录: helloworld(项目名)/src/main/resources/application.properties
server.port=8081
server.servlet.context-path=/helloworld
重新启动服务 并 访问:
方法二: 在src/main/resources下新建 application.yml
server:
port: 8082
servlet:
context-path: /helloworld
代码里引用配置文件的数据---------------
resources/application.yml 代码:
limit:
minMoney: 2
maxMoney: 99
description: 最少要发${limit.minMoney}元, 最多要发${limit.maxMoney}元
helloworld/demo/LimitConfig 代码:----------
package com.helloworld.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
@Component
@ConfigurationProperties(prefix = "limit")
public class LimitConfig {
private BigDecimal minMoney;
private BigDecimal maxMoney;
private String description;
public BigDecimal getMinMoney(){
return minMoney;
}
public BigDecimal getMaxMoney(){
return maxMoney;
}
public String getDescription(){
return description;
}
public void setMinMoney(BigDecimal minMoney){
this.minMoney = minMoney;
}
public void setMaxMoney(BigDecimal maxMoney){
this.maxMoney = maxMoney;
}
public void setDescription(String description){
this.description = description;
}
}
@RestController
public class HelloWorldController {
@Autowired
private LimitConfig limitConfig;
@GetMapping("hello")
public String hello(){
return "这是说明:"+limitConfig.getDescription();
}
}