主程序运行
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
- 推断应用的类型是普通项目还是web项目
- 找并加载所有可用初始化器,设置到initializers属性中
- 找出所有的应用的应用程序监听器,设置到listeners属性中
- 推断并设置main方法的定义类,找到运行的主类
配置文件
springBoot
使用一个全局的配置文件,配置文件的名称是固定的
-
application.propertoes
语法结构:key=value
例如:server.port=8080
-
application.yml
语法结构:key:空格 value
server:
port: 8080
配置文件的作用:修改SpringBoot
自动装配的默认值,因为SpringBoot
在底层都给我们配置好了
yml可以直接给实体类赋值,如下所示
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Dog {
private String name;
private Integer age;
public Dog() {
}
public Dog(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private Boolean student;
private Date birth;
private Map<String,Object> maps;
private List<String> list;
private Dog dog;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getStudent() {
return student;
}
public void setStudent(Boolean student) {
this.student = student;
}
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<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", student=" + student +
", bitch=" + birth +
", maps=" + maps +
", list=" + list +
", dog=" + dog +
'}';
}
}
application.yml中:
#对象
person:
name: 李
age: 20
student: false
birth: 1998/1/1
maps: {k1: v1,k2: v2}
list:
- code
- music
dog:
name: 汪汪汪
age: 1
//程序的主入口
@SpringBootTest
class SpringbootApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
控制台