文章目录
- 零、学习目标
- 一、为什么需要加载自定义配置文件
- 二、使用@PropertySource加载自定义配置文件
- (一)创建Spring Boot项目
- (二)创建自定义配置文件
- (三)创建自定义配置类
- (四)编写测试方法
- (五)运行测试方法
- (六)修改测试方法代码
- (七)再次运行测试方法
- 课堂练习:在Web页面显示学生配置信息
- 三、使用@ImportResource加载XML配置文件
- (一)创建创建Spring Boot项目
- (二)创建自定义服务类
- (三)创建Spring配置文件
- (四)加载自定义Spring配置文件
- (五)编写测试方法
- (六)运行测试方法
- 四、使用@Configuration编写自定义配置类
- (一)创建Spring Boot项目
- (二)创建自定义服务类
- (三)创建自定义配置类
- (四)编写测试方法
- (五)运行测试方法
零、学习目标
- 熟悉使用@PropertySource加载配置文件
- 熟悉使用@ImportResource加载XML配置文件
- 掌握使用@Configuration编写自定义配置类
一、为什么需要加载自定义配置文件
- Spring Boot免除了项目中大部分的手动配置,对于一些特定情况,我们可以通过修改全局配置文件以适应具体生产环境,可以说,几乎所有的配置都可以写在
application.peroperties或application.yaml文件中,Spring Boot会自动加载全局配置文件从而免除我们手动加载的烦恼。但是,如果我们自定义配置文件,Spring Boot是无法识别这些配置文件的,此时就需要我们手动加载。
二、使用@PropertySource加载自定义配置文件
(一)创建Spring Boot项目
-
基于
Spring Initializr创建Spring Boot项目

-
设置项目元数据

-
选择Spring Boot版本,添加相关依赖

-
设置项目名称与保存位置

-
单击【Finish】按钮

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

-
说明:如果在配置文件里使用
user.name,通过配置文件获取的值可能是操作系统中的用户名,因为操作系统中也是有user.name属性的。 -
为了在属性文件里使用中文而不出现乱码,我们需要设置文件编码

-
设置学生的四个属性值

(三)创建自定义配置类
- 在
net.huawei.boot包里创建config子包,在子包里创建StudentConfig

package net.huawei.boot.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* 功能:学生配置类
* 作者:华卫
* 日期:2023年05月30日
*/
@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 +
'}';
}
}
(四)编写测试方法
-
打开自带的测试类
ConfigDemo01ApplicationTests

-
注入学生配置实体,创建
testStudentConfig()测试方法,在里面输出学生配置实体信息
package net.huawei.boot;
import net.huawei.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);
}
}
(五)运行测试方法
- 运行
testStudentConfig()方法,查看结果

(六)修改测试方法代码
- 说明:注入的
StudentConfig名称不必是studentConfig,在Spring Boot里,StudentConfig的注解@Component默认是单例的,因此不会因为注入名称是studentConfig1而产生的两个StudentConfig实例。

(七)再次运行测试方法
- 运行
testStudentConfig()方法,查看结果

- 可以看到,
StudentConfig注入名称改成student之后,测试结果依然相同,不受注入名称变化的任何影响。
课堂练习:在Web页面显示学生配置信息
-
创建
controller子包,在子包里创建StudentConfigController类

-
运行入口类,在浏览器里访问
http://localhost:8080/student

-
或者显示学生配置实体的JSON格式

-
或者逐个字段显示学生配置实体的信息

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

-
设置项目元数据

-
选择Spring Boot版本,添加相关依赖

-
设置项目名称与保存位置

-
单击【Finish】按钮

(二)创建自定义服务类
- 在
net.huawei.boot包里创建service子包,在子包里创建CustomService类

package net.huawei.boot.service;
/**
* 功能:自定义服务类
* 作者:华卫
* 日期:2023年05月30日
*/
public class CustomService {
public void welcome() {
System.out.println("欢迎您访问泸州职业技术学院~");
}
}
(三)创建Spring配置文件
-
在
resources里创建config目录,在config目录里创建spring-config.xml文件

-
在
<beans>元素里添加子元素<bean>,定义自定义服务类的JavaBean
<?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="net.huawei.boot.service.CustomService"/>
</beans>
(四)加载自定义Spring配置文件
-
在入口类上添加注解
@ImportResource("classpath: config/spring-config.xml")

-
在Spring Boot启动后,Spring容器中就会自动实例化一个名为
customService的Bean对象
(五)编写测试方法
-
打开自带的测试类
ConfigDemo02ApplicationTests

-
注入在Spring配置文件里定义的Bean,创建
testCustomService()测试方法,然后调用自定义Bean的方法
package net.huawei.boot;
import net.huawei.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();
}
}
(六)运行测试方法
- 运行
testCustomService()方法,查看结果

四、使用@Configuration编写自定义配置类
- 我们知道,Spring Boot追求的是零配置文件。使用
@Configuration编写自定义配置类,这是Spring Bboot的推荐方式
(一)创建Spring Boot项目
-
基于
Spring Initializr模板创建Spring Boot项目

-
设置项目元数据

-
选择Spring Boot版本,添加相关依赖

-
配置项目名称与保存位置

-
单击【Finish】按钮

(二)创建自定义服务类
- 在
net.huawei.boot包里创建service子包,在子包里创建CustomService类

package net.huawei.boot.service;
/**
* 功能:自定义服务类
* 作者:华卫
* 日期:2023年05月30日
*/
public class CustomService {
public void welcome() {
System.out.println("欢迎您访问泸州职业技术学院~");
}
}
(三)创建自定义配置类
-
在
net.huawei.boot包里创建config子包,在子包里创建CustomConfig类

-
添加注解
@Configuration,指定配置类

-
创建获取Bean的
getCustomService()方法
package net.huawei.boot.config;
import net.huawei.boot.service.CustomService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 功能:自定义配置类
* 作者:华卫
* 日期:2023年05月30日
*/
@Configuration
public class CustomConfig {
@Bean(name = "cs") // 指定Bean的名称`cs`,否则采用默认名称`customService`
public CustomService getCustomService() {
return new CustomService();
}
}
(四)编写测试方法
- 打开自带的测试类
ConfigDemo03ApplicationTests

- 注入在CustomConfig配置类里定义的Bean,创建
testCustomService()方法,然后调用自定义Bean的方法
package net.huawei.boot;
import net.huawei.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 ConfigDemo03ApplicationTests {
@Autowired // 注入自定义服务实体
private CustomService cs;
@Test
public void testCustomService() {
// 调用自定义服务实体的方法
cs.welcome();
}
}
(五)运行测试方法
- 运行
testCustomService()方法,查看结果



















