一、使用@PropertySource加载自定义配置文件
1.1 创建Spring Boot项目
- 创建
Spring Boot项目


 - 单击【创建】按钮

 
1.2 创建自定义配置文件
- 在
resources里创建myconfig.properties文件

 - 设置文件编码

 - 设置学生的四个属性值

 
1.3 创建自定义配置类
- 在
cn.kox.boot包里创建config子包,在子包里创建StudentConfig

 
package cn.kox.boot.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component // Spring容器组件
@PropertySource("classpath:myconfig.properties") // 加载自定义配置文件
@ConfigurationProperties(prefix = "student") // 配置属性,设置前缀
public class StudentConfig {
    private String id; // 学号
    private String name; // 姓名
    private String gender; // 性别
    private int age; // 年龄
    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;
    }
    @Override
    public String toString() {
        return "StudentConfig{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                '}';
    }
}
 
1.4 编写测试方法
- 打开自带的测试类
ConfigDemo01ApplicationTests

 - 注入学生配置实体,创建
testStudentConfig()测试方法,在里面输出学生配置实体信息 
package cn.kox.boot;
import cn.kox.boot.config.StudentConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ConfigDemo01ApplicationTests {
    @Autowired // 自动装配学生配置实体
    private StudentConfig studentConfig;
    @Test
    public void testStudentConfig() {
        // 输出学生配置实体信息
        System.out.println(studentConfig);
    }
}
 
1.5 运行测试方法
- 运行
testStudentConfig()方法,查看结果

 
课堂练习:在Web页面显示学生配置信息
- 创建·controller·子包,在子包里创建·StudentConfigController·类

 
package cn.kox.boot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * @ClassName: StudentConfigController
 * @Author: Kox
 * @Data: 2023/6/13
 * @Sketch:
 */
@Controller
public class StudentConfigController {
    @ResponseBody
    @GetMapping("student")
    public String student() {
        return "<p>StudentConfig{id='2021010', name='张三风‘,gender='男’,age=18</p>";
    }
}
 

二、使用@ImportResource加载XML配置文件
2.1 创建创建Spring Boot项目
- 基于Spring Initializr模板创建Spring Boot项目



 
2.2 创建自定义服务类
- 在
cn.kox.boot包里创建service子包,在子包里创建CustomService类

 
package cn.kox.boot.service;
/**
 * @ClassName: CustomService
 * @Author: Kox
 * @Data: 2023/6/13
 * @Sketch:
 */
public class CustomService {
    public void welcome() {
        System.out.println("欢迎您访问泸州职业技术学院~");
    }
}
 
2.3 创建Spring配置文件
- 在
resources里创建config目录,在config目录里创建spring-config.xml文件

 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="customService" class="cn.kox.boot.service.CustomService"/>
</beans>
 
2.4 加载自定义Spring配置文件
- 在入口类上添加注解
@ImportResource("classpath: config/spring-config.xml")

 
2.5 编写测试方法
- 打开自带的测试类
ConfigDemo02ApplicationTests

 
package cn.kox.boot;
import cn.kox.boot.service.CustomService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ConfigDemo02ApplicationTests {
	@Autowired // 注入自定义服务实体
	private CustomService customService;
	@Test
	public void testCustomService() {
		// 调用自定义服务实体的方法
		customService.welcome();
	}
}
 
三、使用@Configuration编写自定义配置类
3.1 创建Spring Boot项目
- 基于Spring Initializr模板创建Spring Boot项目


 



















