一、为什么需要加载自定义配置文件
- Spring Boot免除了项目中大部分的手动配置,对于一些特定情况,我们可以通过修改全局配置文件以适应具体生产环境,可以说,几乎所有的配置都可以写在application.peroperties或application.yaml文件中,Spring Boot会自动加载全局配置文件从而免除我们手动加载的烦恼。但是,如果我们自定义配置文件,Spring Boot是无法识别这些配置文件的,此时就需要我们手动加载。
二、使用@PropertySource加载自定义配置文件
(一)创建Spring Boot项目
- 基于Spring Initializr创建Spring Boot项目
- 单击完成按钮
(二)创建自定义配置文件
-
在resources里创建myconfig.properties文件
-
说明:如果在配置文件里使用user.name,通过配置文件获取的值可能是操作系统中的用户名,因为操作系统中也是有user.name属性的。
-
为了在属性文件里使用中文而不出现乱码,我们需要设置文件编码
-
设置学生的四个属性值
(三)创建自定义配置类
- 在net.wtt.boot包里创建config子包,在子包里创建StudentConfig
package net.wtt.boot.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@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;
}
@Override
public String toString() {
return "StudentConfig{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
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;
}
}
(四)编写测试方法
- 打开自带的测试类ConfigDemo01ApplicationTests
package net.wtt.boot;
import net.wtt.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项目
- 单击完成按钮,创建项目
(二)创建自定义服务类 - 在net.wtt.boot包里创建service子包,在子包里创建CustomService类
package net.wtt.boot.service;
public class CustomService {
public void welcome() {
System.out.println("欢迎您访问泸州职业技术学院~");
}
}
(三)创建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="net.wtt.boot.service.CustomService"/>
</beans>
(四)加载自定义Spring配置文件
- 在入口类上添加注解@ImportResource(“classpath: config/spring-config.xml”)
- 在Spring Boot启动后,Spring容器中就会自动实例化一个名为customService的Bean对象
(五)编写测试方法
- 打开自带的测试类ConfigDemo02ApplicationTests
- 注入在Spring配置文件里定义的Bean,创建testCustomService()测试方法,然后调用自定义Bean的方法
package net.wtt.boot;
import net.wtt.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项目
ConfigDemo03创建方式同上
(二)创建自定义服务类
- 在net.huawei.boot包里创建service子包,在子包里创建CustomService类
package net.wtt.boot.service;
public class CustomService {
public void welcome() {
System.out.println("欢迎您访问泸州职业技术学院~");
}
}
(三)创建自定义配置类
- 在net.wtt.boot包里创建config子包,在子包里创建CustomConfig类
- 添加注解@Configuration,指定配置类
- 创建获取Bean的getCustomService()方法
package net.wtt.boot.config;
import net.wtt.boot.service.CustomService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomConfig {
@Bean(name = "cs") // 指定Bean的名称`cs`,否则采用默认名称`customService`
public CustomService getCustomService() {
return new CustomService();
}
}
(四)编写测试方法
- 打开自带的测试类ConfigDemo03ApplicationTests
- 注入在CustomConfig配置类里定义的Bean,创建testCustomService()方法,然后调用自定义Bean的方法
package net.wtt.boot;
import net.wtt.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()方法,查看结果