版权声明:本文为博主原创文章,未经博主允许不得转载。你想转载请附加连接哦 https://blog.csdn.net/dmw412724/article/details/89641678
点击返回目录
在web开发里,我们通常将数据库的用户名/密码/url/driver的信息写到配置文件里。
本章教程就是教大家如何获取配置文件里的键值对。
application.properties里写上
aaa=111
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/te
jdbc.username=root
jdbc.password=123
单值引入:
@RestController public class MyController { @Value(value = "${aaa}") private String hello; @RequestMapping("") public String hello(){ return hello; } }
在bean的属性上使用@Value注解,可以将配置文件的值绑定到属性上。
多值引入:
@Configuration public class MybatisConfig { @Bean @ConfigurationProperties(prefix = "jdbc") DataSource dataSource() { return new DruidDataSource(); } }
在bean上打上注解@ConfigurationProperties,写上前缀即可。