Spring Boot基础学习笔记03:Spring Boot两种全局配置和两种注解
创建一个springboot项目
在application.yaml里添加相关配置
1、配置tomcat端口号和web虚拟路径
#修改tomcat默认端口号
server:
port: 8888
#修改web虚拟路径
servlet:
context-path: /lzy
- 启动应用,查看控制台
2、对象类型的配置与使用
(1)创建Pet类
package com.wh.demo.enity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "person.pet")
public class Pet {
// @Value("${pet.type}")
private String type; // 类型
// @Value("${pet.name}")
private String name; // 名字
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Pet{" +
"type='" + type + '\'' +
", name='" + name + '\'' +
'}';
}
}
(2)创建Person类
package com.wh.demo.enity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
public class person {
private int id; // 编号
private String name; // 姓名
private List<String> hobby; // 爱好;
private Map<String, String> family; // 家庭成员
private Pet pet; // 宠物
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public Map<String, String> getFamily() {
return family;
}
public void setFamily(Map<String, String> family) {
this.family = family;
}
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
@Override
public String toString() {
return "person{" +
"id=" + id +
", name='" + name + '\'' +
", hobby=" + hobby +
", family=" + family +
", pet=" + pet +
'}';
}
}
(3)在application.yaml里配置对象
#配置对象
person:
id: 1
name: 张三丰
hobby: 旅游,美食,音乐
family:
father: 张云光
mother: 吴文燕
grandpa: 张宏宇
grandma: 唐雨欣
son: 张君宝
daughter: 张晓敏
pet:
type: 泰迪犬
name: 瑞瑞
(4)给Person类添加注解
-
添加注解@Component,交给Spring去管理
-
添加注解@ConfigurationProperties(prefix = “person”)
- 注意:采用
@ConfigurationProperties
注解方式,必须要有set方法
,才会自动为Person类所有属性注入相应的值,包括简单类型和复杂类型
(5)给Pet类添加注解
- 添加注解@Component,交给Spring去管理
- 添加注解@ConfigurationProperties(prefix = “person.pet”) - 可以不用添加
(6)从Spring容器里获取Person类的实例并输出
- 实现接口ApplicationContextAware,实现其抽象方法setApplicationContext
- 声明ApplicationContext对象,并在setApplicationContext里初始化
- 创建测试方法testPerson(),从Spring容器中获取Person类的实例并输出
- 运行测试方法testPerson(),查看结果
(8)从Spring容器里获取Pet类的实例并输出
- 在测试类里添加测试方法testPet()
- 运行测试方法testPet(),查看结果
- 给Pet类的属性添加值注解@Value
- 再次运行测试方法testPet(),查看结果
3、两种属性注解方式的对比
- 采用@ConfigurationProperties注解方式,必须要有set方法,才会自动为所注解的类的全部属性注入相应的值,包括简单类型和复杂类型(List、Map、Pet……)。
- 采用@Value注解方式,优点在于可以不要set方法,但是有两点不足:其一、需要一个一个地注入,显得麻烦;其二、对于复杂类型不能注入,比如Map、List、Pet等。
课后作业
任务:修改StudentInfo项目输出学生信息
- 创建学生实体类Student
编写student类
package com.wh.demo.enity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "student")
public class student {
private String id;
private String name;
private String gender;
private int age;
private String major;
private String telephone;
private String email;
private String hobby;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", major='" + major + '\'' +
", telephone='" + telephone + '\'' +
", email='" + email + '\'' +
", hobby='" + hobby + '\'' +
'}';
}
}
配置student对象属性
student:
id: 19204030
name: 李晓好昂
gender: 男
age: 19
major: 大数据2班
telephone: 15825702580
email: maset@qq.com
hobby: 玩
修改控制器,student()方法里返回student的值
运行启动类
配置student对象属性
student:
id: 19204030
name: 李晓好昂
gender: 男
age: 19
major: 大数据2班
telephone: 15825702580
email: maset@qq.com
hobby: 玩