Spring Boot基础学习笔记04:Spring Boot加载自定义配置文件

零、学习目标
1、熟悉使用@PropertySource加载配置文件
2、熟悉使用@ImportResource加载XML配置文件
3、掌握使用@Configuration编写自定义配置类
一、为什么需要加载自定义配置文件

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

二、使用@PropertySource加载自定义配置文件
(一)创建Spring Boot Web项目ConfigDemo01

    设置项目元数据
   

在这里插入图片描述

    添加项目依赖
在这里插入图片描述

    设置项目名称与保存位置
 

在这里插入图片描述

    完成项目初始化工作

在这里插入图片描述

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

  • 在resources下创建myconfig.properties文件

在这里插入图片描述

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

在这里插入图片描述

(三)创建自定义配置类

  • 在net.hw.lesson04包里创建配置类StudentConfig

在这里插入图片描述

package net.hw.lesson04.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * 功能:学生配置类
 * 作者:华卫
 * 日期:2021年05月07日
 */
@Component // 交给Spring容器管理
@PropertySource("classpath:myconfig.properties") // 加载自定义配置文件
@ConfigurationProperties(prefix="student") // 配置属性,设置前缀
public class StudentConfig {
    private String id;
    private String name;
    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "StudentConfig{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

(四)编写测试方法

  • 点开测试类ConfigDemo01ApplicationTes

在这里插入图片描述

编写测试方法,注入学生配置实体,创建testStudentConfig()测试方法,在里面输出学生配置实体信息

package net.hw.lesson04;

import net.hw.lesson04.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
    void contextLoads() {
    }
    
    @Test
    public void testStudentConfig() {
        // 输出学生配置实体信息
        System.out.println(studentConfig);
    }
}

(五)运行测试方法

在这里插入图片描述

(六)修改测试方法代码

  • 说明:注入的StudentConfig名称不必是studentConfig,在Spring Boot 2.4.5里,StudentConfig的注解@Component默认是单例的,因此不会因为注入名称是studentConfig1而产生的两个StudentConfig实例。

在这里插入图片描述

(七)再次运行测试方法

在这里插入图片描述

  • 可以看到,StudentConfig注入名称改成studentConfig1之后,测试结果依然相同,不受注入名称变化的任何影响。

课堂练习:在Web页面显示学生配置信息

  • 创建controller子包,在子包里控制器ConfigDemo01Controller

在这里插入图片描述

运行启动类ConfigDemo01Application

在这里插入图片描述

在浏览器里访问http://localhost:8080/student

在这里插入图片描述

三、使用@ImportResource加载XML配置文件

(一)创建创建Spring Boot Web项目ConfigDemo02

  • 设置项目元数据

在这里插入图片描述

添加项目依赖

在这里插入图片描述

设置项目名称与保存位置

在这里插入图片描述

完成项目初始化工作

在这里插入图片描述

(二)创建自定义服务类

  • 在net.hw.lesson04包里创建service子包,在子包里创建CustomService类

在这里插入图片描述

package net.hw.lesson04.service;

import org.springframework.stereotype.Service;

/**
 * 功能:自定义服务类
 * 作者:华卫
 * 日期:2021年05月07日
 */
public class CustomService {
    public void welcome() {
        System.out.println("欢迎您访问泸州职业技术学院~");
    }
}

(三)创建Spring配置文件

  • 在resources目录里创建配置文件spring-config.xml

在这里插入图片描述

在这里插入图片描述

  • 元素里添加子元素,定义自定义服务类的JavaBean
<bean name="customService" class="net.hw.lesson04.service.CustomService"></bean>
  • 定义一个Bean,指定Bean的名称及类所在的路径

在这里插入图片描述

(四)在启动类上添加注解,加载自定义Spring配置文件

  • 在启动类上添加注解@ImportResource("classpath:spring-config.xml")

在这里插入图片描述

  • 在Spring Boot启动后,Spring容器中就会自动实例化一个名为customService的Bean对象

(五)打开测试类,编写测试方法

  • 点开测试类ConfigDemo02ApplicationTests

在这里插入图片描述

注入在Spring配置文件里定义的Bean,创建testCustomService测试方法,然后调用自定义Bean的方法

package net.hw.lesson04;

import net.hw.lesson04.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
    void contextLoads() {
    }

    @Test
    public void testCustomService() {
        // 调用自定义服务实体的方法
        customService.welcome();
    }
}

(六)运行测试方法,查看结果

在这里插入图片描述

四、使用@Configuration编写自定义配置类

  • 使用@Configuration编写自定义配置类,这是Spring Bboot的推荐方式

(一)创建Spring Boot Web项目ConfigDemo03

  • 设置项目元数据

在这里插入图片描述

添加项目依赖

在这里插入图片描述

配置项目名称与保存位置

在这里插入图片描述

完成项目初始化工作
 

在这里插入图片描述

(二)2、创建自定义服务类

  • 在net.hw.lesson04包里创建service子包,在子包里创建CustomService类

在这里插入图片描述

package net.hw.lesson04.service;

/**
 * 功能:自定义服务类
 * 作者:华卫
 * 日期:2021年05月07日
 */
public class CustomService {
    public void welcome() {
        System.out.println("欢迎您访问泸州职业技术学院~");
    }   
}

(三)创建自定义配置类CustomConfig

  • 在net.hw.lesson04包里创建config子包,在子包里创建自定义配置类CustomConfig

在这里插入图片描述

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


在这里插入图片描述

创建获取Bean的方法getCustomService()

在这里插入图片描述

(四)打开测试类,编写测试方法

  • 点开测试类ConfigDemo03ApplicationTests

在这里插入图片描述

注入在CustomConfig配置类里定义的Bean,创建测试方法testCustomService(),然后调用自定义Bean的方法

package net.hw.lesson04;

import net.hw.lesson04.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 customerService;

    @Test
    void contextLoads() {
    }

    @Test
    public void testCustomService() {
        customerService.welcome();
    }
}

(五)运行测试方法,查看结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值