夜光序言:
有些人对你好,是先甜后苦,一开始你觉得爱死了,如胶似漆,但到最后就越来越淡,很快分手。而有些人就是慢热型的,可接触的时间越长,他就对你越好。因为爱你在开始的人,是想得到你,爱你到最后的人,才是想陪着你。
正文:
以道御术 / 以术识道
package com.example.springboot02config.pojo;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @Description: 人类实体类
* @Author: Hy
* @Date: 2020/1/19
*/
@Component //实体类,我们都需要添加一个这个注解
public class Person {
private String name;
private int age;
private boolean happy;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
//首先,来一个无参构造
public Person() {
}
//之后呢,再来一个有参构造
public Person(String name, int age, boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
this.name = name;
this.age = age;
this.happy = happy;
this.birth = birth;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}
//下面就是:get和set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isHappy() {
return happy;
}
public void setHappy(boolean happy) {
this.happy = happy;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
}
package com.example.springboot02config.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @Description: Dog 实体类
* @Author: Hy
* @Date: 2020/1/19
*/
@Component //实体类,我们都需要添加一个这个注解
public class Dog {
@Value("小炎")
private String name;
@Value("11")
private int age;
public Dog(){
}
//构造器
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//再写一个tostring方法
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.example.springboot02config;
import com.example.springboot02config.pojo.Dog;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot02ConfigApplicationTests {
@Autowired
// @Qualifier //如果有很多dog,我们可以添加这个注解,实现指定
private Dog dog;
@Test
void contextLoads() {
//夜光:我们输出一下,试试
System.out.println(dog);
}
}
package com.example.springboot02config.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @Description: 人类实体类
* @Author: Hy
* @Date: 2020/1/19
*/
@Component //实体类,我们都需要添加一个这个注解
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age;
private boolean happy;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
//首先,来一个无参构造
public Person() {
}
//之后呢,再来一个有参构造
public Person(String name, int age, boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
this.name = name;
this.age = age;
this.happy = happy;
this.birth = birth;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}
//下面就是:get和set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isHappy() {
return happy;
}
public void setHappy(boolean happy) {
this.happy = happy;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
##夜光:
##springboot这个配置文件里面到底可以放哪些参数呢?
#
##我们看下官网,发现官方的配置特别多
#
##了解原理,一通百通
##普通的key-value
#name: black
#
##当然,也可以存一个对象
##这个里面的空格,是有层级关系的
##对空格的要求很高~
##有一个强大之处,可以注入到我们写的配置类中
#student:
# name: Genius 01
# age: 26
#
##这里还有一个行内写法
##有点类似于JavaScript
#student02: {name: Genius 02,age: 26}
#
##数组
#GeniusTeam:
# - 01
# - 02
# - 03
#
#GeniusTeam02: [01,02,03]
#之前,我们也说了,可以保存对象
person:
name: 萧炎
age: 26
happy: true
birth: 2020/1/19
maps: {k1: v1,k2: v2} #键值对
lists:
- code
- music
- draw
- girlfriend
dog:
name: 小炎
age: 11
package com.example.springboot02config.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @Description: 人类实体类
* @Author: Hy
* @Date: 2020/1/19
*/
@Component //实体类,我们都需要添加一个这个注解
//@ConfigurationProperties(prefix = "person")
//javaconfig 绑定我们配置文件的值,可以采取这些方式
//夜光:加载指定的配置文件
@PropertySource(value = "classpath:yeguang.properties")
public class Person {
//SPEL表达式取出配置文件的值
@Value("${name}")
private String name;
private int age;
private boolean happy;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
//首先,来一个无参构造
public Person() {
}
//之后呢,再来一个有参构造
public Person(String name, int age, boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
this.name = name;
this.age = age;
this.happy = happy;
this.birth = birth;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}
//下面就是:get和set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isHappy() {
return happy;
}
public void setHappy(boolean happy) {
this.happy = happy;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}