Java企业级开发学习笔记(4.4)Spring Boot加载自定义配置文件

本文介绍了如何在SpringBoot项目中使用@PropertySource加载自定义的properties配置文件,并通过@ConfigurationProperties绑定属性到Java对象。此外,还展示了如何利用@ImportResource加载XML配置文件,并在测试及实际应用中使用这些配置。
摘要由CSDN通过智能技术生成

一、使用@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项目
    在这里插入图片描述
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值