Spring Boot 加载自定义配置文件


一、为什么需要加载自定义配置文件

在这里插入图片描述

Spring Boot免除了项目中大部分的手动配置,对于一些特定情况,我们可以通过修改全局配置文件以适应具体生产环境,可以说,几乎所有的配置都可以写在application.peroperties或application.yaml文件中,Spring Boot会自动加载全局配置文件从而免除我们手动加载的烦恼。但是,如果我们自定义配置文件,Spring Boot是无法识别这些配置文件的,此时就需要我们手动加载。

二、使用@PropertySource加载自定义配置文件

(一)创建Spring Boot项目

使用Spring Initializr创建Spring Boot项目——ConfigDemo01

配置好后,单击【Next】按钮
在这里插入图片描述选择Spring Boot版本,添加相关依赖,单击【Create】按钮
在这里插入图片描述
在这里插入图片描述

(二)创建自定义配置文件

在resources里创建myconfig.properties文件
在这里插入图片描述

说明:如果在配置文件里使用user.name,通过配置文件获取的值可能是操作系统中的用户名,因为操作系统中也是有user.name属性的。

为了在属性文件里使用中文而不出现乱码,我们需要设置文件编码
在这里插入图片描述
设置学生的四个属性值
在这里插入图片描述

student.id=20230101
student.name=张三丰
student.gender=男
student.age=18

(三)创建自定义配置类

在net.army.boot包里创建config子包,在子包里创建StudentConfig
在这里插入图片描述

(四)编写测试方法

打开自带的测试类ConfigDemo01ApplicationTests
在这里插入图片描述
注入学生配置实体,创建testStudentConfig()测试方法,在里面输出学生配置实体信息

package net.army.boot;

import net.army.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项目——ConfigDemo02,配置完成后,单击【Next】按钮
在这里插入图片描述

选择Spring Boot版本,添加相关依赖,单击【Create】按钮
在这里插入图片描述
在这里插入图片描述

(二)创建自定义服务类

在net.army.boot包里创建service子包,在子包里创建CustomService类
在这里插入图片描述

package net.army.boot.service;

/**
 * 作者:梁辰兴
 * 日期:2023/5/31
 * 功能:自定义服务类
 */
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.army.boot.service.CustomService"/>

</beans>

(四)加载自定义Spring配置文件

在入口类上添加注解@ImportResource(“classpath:config/spring-config.xml”)
在这里插入图片描述
在Spring Boot启动后,Spring容器中就会自动实例化一个名为customService的Bean对象

(五)编写测试方法

打开自带的测试类ConfigDemo02ApplicationTests
在这里插入图片描述注入在Spring配置文件里定义的Bean,创建testCustomService()测试方法,然后调用自定义Bean的方法

package net.army.boot;

import net.army.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,配置好后,单击【Next】按钮
在这里插入图片描述

选择Spring Boot版本,添加相关依赖,单击【Create】按钮
在这里插入图片描述在这里插入图片描述

(二)创建自定义服务类

在net.army.boot包里创建service子包,在子包里创建CustomService类
在这里插入图片描述

package net.army.boot.service;

/**
 * 作者:梁辰兴
 * 日期:2023/5/31
 * 功能:自定义服务类
 */
public class CustomService {
    
    
    public void welcome() {
    
    
        System.out.println("欢迎您访问泸州职业技术学院~");
    }
}

(三)创建自定义配置类

在net.army.boot包里创建config子包,在子包里创建CustomConfig类
在这里插入图片描述

添加注解@Configuration,指定配置类
在这里插入图片描述创建获取Bean的getCustomService()方法

package net.army.boot.config;

import net.army.boot.service.CustomService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 作者:梁辰兴
 * 日期:2023/5/31
 * 功能:自定义配置类
 */
@Configuration
public class CustomConfig {
    
    
    @Bean(name = "cs") // 指定Bean的名称`cs`,否则采用默认名称`customService`
    public CustomService getCustomService() {
    
    
        return new CustomService();
    }
}

(四)编写测试方法

打开自带的测试类ConfigDemo03ApplicationTests
在这里插入图片描述
注入在CustomConfig配置类里定义的Bean,创建testCustomService()方法,然后调用自定义Bean的方法

package net.army.boot;

import net.army.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()方法,查看结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_62617719/article/details/130965226