1、Springboot 总结
1、配置
1.1、读取配置
lesson: SpringBoot
server:
port: 82
enterprise:
name: itcast
age: 16
tel: 4006184000
subject:
- Java
- 前端
- 大数据
// 读取数据
@Value("${lesson}")
private String lesson;
// 使用自动装配将所有的数据封装到 env 中
@Autowired
private Environment env;
System.out.println(env.getProperty("lesson"));
System.out.println(env.getProperty("enterprise.name"));
System.out.println(env.getProperty("enterprise.subject[0]"));
1.2、自定义对象封装指定数据
- 使用 @ConfigurationProperties 注解绑定配置信息到封装类中
- 封装类需要定义为 Spring 管理的 bean,否则无法进行属性注入
# application.yml
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
username: root
password: root
// @Component
@Configuration
@ConfigurationProperties(prefix = "datasource")
public class MyDataSource {
private String driverClassName;
private String url;
private String userName;
private String password;
// setter()、getter() 方法
}
@Autowired
private MyDataSource myDataSource;
本文来自博客园,作者:lidongdongdong~,转载请注明原文链接:https://www.cnblogs.com/lidong422339/p/17609517.html

浙公网安备 33010602011771号