springboot 学习笔记(三)属性注入
2019-08-03 本文已影响0人
囝囝123
配置文件使用的application.yml
jdbc:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: root
user:
name: jack
age: 22
language:
- java
- php
定义读取配置文件的类
package com.liu.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
//配置文件的前缀 prefix
@ConfigurationProperties(prefix = "jdbc")
@Data
@Component
public class jdbcProperties {
String url;
String driverClassName;
String username;
String password;
User user = new User();
@Data
class User{
String name;
int age;
List<String> language;
}
}
定义方法测试一下数据
@RestController
public class HelloWebController {
@Autowired
private jdbcProperties jdbcProperties;
@RequestMapping("/hello")
public Object hello(){
return jdbcProperties;
}
}
读取配置文件的方式有很多种,我觉得这种挺好用哈哈哈!