SpringBoot读取配置文件参数

  sre

5fdb7cdbc6fae2756.jpg_fo742.jpg

application-dev.yml 配置

  datasource:
    primary:
      jdbc-url: jdbc:mysql://localhost:3306/dbname
      #url: jdbc:mysql://localhost:3306/dbname
      username: root
      password: 123456
      driver-class-name: com.mysql.jdbc.Driver

spring.datasource.primary.password

ParamConfig.java 读取

@Component
public class ParamConfig {
    @Value("{spring.datasource.primary.jdbc-url}")
    private String jdbc-url ;
    @Value("{spring.datasource.primary.username}")
    private String username ;
    @Value("${spring.datasource.primary.password}")
    private String password ;
...
}

ParamController.java 调用

@RestController
public class ParamController {

    @Resource
    private ParamConfig paramConfig ;

    @RequestMapping("/getParam")
    public String getParam (){
        return "["+paramConfig.getJdbc-url()+";"+
                paramConfig.getUsername()+";"+
                paramConfig.getPassword()+"]" ;
    }
}

LEAVE A COMMENT

Captcha Code