企业级信息系统开发——Spring Boot加载自定义配置文件

news2024/10/6 16:17:02

文章目录

  • 一、使用@PropertySource加载自定义配置文件
    • (一)创建Spring Boot Web项目ConfigDemo01
    • (二)创建自定义配置文件
    • (三)创建自定义配置类
    • (四)编写测试方法
    • (五)运行测试方法
    • (六)修改测试方法代码
    • (七)再次运行测试方法
    • 课堂练习:在Web页面显示学生配置信息
  • 二、使用@ImportResource加载XML配置文件
    • (一)创建创建Spring Boot Web项目ConfigDem
    • (二)创建自定义服务类
    • (三)创建Spring配置文件
    • (四)在启动类上添加注解,加载自定义Spring配置文件
    • (五)打开测试类,编写测试方法
    • (六)运行测试方法,查看结果
  • 三、使用@Configuration编写自定义配置类
    • (一)创建Spring Boot Web项目ConfigDemo03
    • (二)创建自定义服务类
    • (三)创建自定义配置类CustomConfig
    • (四)打开测试类,编写测试方法
    • (五)运行测试方法,查看结果

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

(一)创建Spring Boot Web项目ConfigDemo01

  • 设置项目元数据
    在这里插入图片描述
  • 添加项目依赖
    在这里插入图片描述
  • 设置项目编码为utf8(尤其注意复选框)
    在这里插入图片描述

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

  • resources下创建myconfig.properties文件
student.id=20210101
student.name=张三丰
student.gender=男
student.age=18

(三)创建自定义配置类

  • net.shuai.boot包里创建配置类config.StudentConfig
package nei.shuai.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;

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = 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 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 nei.shuai.boot;

import nei.shuai.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);
    }

}

(五)运行测试方法

在这里插入图片描述

(六)修改测试方法代码

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

(七)再次运行测试方法

在这里插入图片描述

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

  • 创建controller子包,在子包里控制器StudentConfigController
package nei.shuai.boot.controller;

import nei.shuai.boot.config.StudentConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentConfigController {

    @Autowired // 自动装配学生配置实体
    private StudentConfig studentConfig;

    @RequestMapping("/student")
    @ResponseBody
    public String student() {
        return studentConfig.toString();
    }
}
  • 运行启动类StudentConfigController
  • 在浏览器里访问http://localhost:8080/student
    在这里插入图片描述
  • 显示学生对象JSON
package nei.shuai.boot.controller;

import nei.shuai.boot.config.StudentConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentConfigController {

    @Autowired // 自动装配学生配置实体
    private StudentConfig studentConfig;

    @RequestMapping("/student")
    @ResponseBody
    public StudentConfig student() {
        return studentConfig;
    }
}
  • 在浏览器里访问http://localhost:8080/student
    在这里插入图片描述
  • 格式化字符串
package nei.shuai.boot.controller;

import nei.shuai.boot.config.StudentConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentConfigController {

    @Autowired // 自动装配学生配置实体
    private StudentConfig studentConfig;

    @RequestMapping("/student")
    @ResponseBody
    public String student() {
        return "学号:" + studentConfig.getId() + "<br/>" +
                "姓名:" + studentConfig.getName() + "<br/>" +
                "性别:" + studentConfig.getGender() + "<br/>" +
                "年龄:" + studentConfig.getAge();
    }
}
  • 在浏览器里访问http://localhost:8080/student
    在这里插入图片描述

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

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

  • 设置项目元数据
    在这里插入图片描述
  • 添加项目依赖
    在这里插入图片描述

(二)创建自定义服务类

  • net.shuai.boot包里创建service子包,在子包里创建CustomService
package net.shuai.boot.service;

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

(三)创建Spring配置文件

  • resources目录里创建配置文件spring-config.xml
    在这里插入图片描述
  • <beans>元素里添加子元素<bean>,定义自定义服务类的JavaBean
<bean id="customService" class="net.shuai.boot.service.CustomService"/>
  • 定义一个Bean,指定Bean的名称及类所在的路径
    在这里插入图片描述

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

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

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

  • 点开测试类ConfigDemo02ApplicationTests
package net.shuai.boot;

import net.shuai.boot.service.CustomService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Currency;

@SpringBootTest
class ConfigDemo02ApplicationTests {

    @Autowired // 注入自定义的服务实体类
    private CustomService customService;

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

}

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

在这里插入图片描述

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

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

(一)创建Spring Boot Web项目ConfigDemo03

  • 设置项目元数据
    在这里插入图片描述
  • 添加项目依赖
    在这里插入图片描述

(二)创建自定义服务类

  • net.shuai.boot包里创建service子包,在子包里创建CustomService
package net.shuai.boot.service;

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

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

  • net.shuai.boot包里创建config子包,在子包里创建自定义配置类CustomConfig
  • 添加注解@Configuration,指定配置类
    在这里插入图片描述
  • 创建获取Bean的方法getCustomService()
    在这里插入图片描述
package net.shuai.boot.config;

import net.shuai.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.shuai.boot;

import net.shuai.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 customService;

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

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

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/588299.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

一键部署属于自己的ChatGPT-Next-Web

完整功能刚需&#xff1a; OpenAI 注册登录之后给的 api Key GitHub账号 Netlify账号 Tip&#xff1a; 注册 OepenAI账号 需要用国外手机号 这里建议去一些渠道购买账号 十块钱不到如果访问 OpenAI 的话 一定要挂欧美节点 否则禁止IP访问 概率会被封号为什么用 Netlify 托…

测试替身Test Doubles的5类型(Mockito)

测试替身Test Doubles的5类型(Mockito) 我们有一个名为 BankAccount 的类。 数据库用于存储和检索银行帐户信息。 我们想测试 BankAccount 中的逻辑&#xff0c;而不必担心它使用的底层数据库.由此类实现——它将 SQL 查询发送到数据库并返回其中包含的值。 测试替身Test Dou…

SuperMap iDesktopX扩展开发之GPA算子扩展

作者&#xff1a;dongyx SuperMap iDesktopX是超图研究院推出的一款跨平台的桌面GIS软件&#xff0c;兼容Windows和Linux&#xff0c;同时iDesktopX也采用的是插件式扩展开发框架&#xff0c;支持定制开发。 使用iDesktopX定制开发有以下优势&#xff1a; ⚫ 采用 Swing 图形界…

VM虚拟机仿真网络问题

在电子数据取证中&#xff0c;拿到一个镜像需要仿真的时候&#xff0c;经常会遇到网络问题。尤其是Linux服务器镜像&#xff0c;例如centos操作系统的镜像&#xff0c;一般镜像会有固定IP设置&#xff0c;仿真起来后&#xff0c;系统与本机不能建立连接&#xff0c;不能连接互联…

VSCode远程连接Ubuntu使用LLDB调试程序

VSCode已经具有远程开发的能力&#xff0c;可以使用SSH连接到Linux/MacOS进行远程开发&#xff0c;包括编译与调试&#xff0c;只需要安装Remote Development插件即可&#xff0c;如果想使用CMake管理项目&#xff0c;则需要将VSCode的CMake以及CMake Tools插件安装在远程机器上…

SpringBoot自定义打印横幅

众所周知&#xff0c;springboot项目启动的时候会打印横幅&#xff0c;横幅内容就是spring; 而spring boot提供了一个Banner接口用于处理启动横幅&#xff0c;默认情况下启动会打印如下信息 . ____ _ __ _ _/\\ / ____ __ _ _(_)_ __ __ _ \ \ \ \ ( (…

分布式系统

一.分布式理论基础 1.CAP理论 CAP定理是分布式系统中的重要理论&#xff0c;在一个分布式系统中最多只能同时满足一致性&#xff08;Consistency&#xff09;、可用性&#xff08;Availability&#xff09;和分区容错性&#xff08;Partition tolerance&#xff09;这三项中的…

以太网驱动的流程浅析(五)-mii_bus初始化以及phy id的获取

【硬件环境】 Imx6ul 【Linux kernel版本】 Linux4.1.15 【以太网phy】 Realtek8201f 1.1. 以太网驱动probe流程 1.1 mii_bus初始化以及phy id的获取 然后进行mii的一些初始化fec_enet_mii_init(pdev); 主要是对struct mii_bus这里的成员进行初始化 并且会做注册mdiobus的…

小笔记-简单但够用系列_jupyter notebook 的重新安装问题

文章目录 目的目标步骤 目的 做程序开发时&#xff0c;想到 jupyter notebook 的浏览器交互式执行&#xff0c;决定再次启用放置许久的 jupyter notebook。 但太久没有执行的 jupyter notebook 在打开页面有一旦打开或创建新的 python&#xff0c;就自动报错退出。 使用过往经…

Blender UV展开流程

目录 1. UV1.1 blender默认物体1.2 创建物体1.3 UV参考图1.4 标记缝合边1.5 UV拉伸1.6 孤岛模式 1. UV 1.1 blender默认物体 默认物体已经自动生成UV 在UV编辑工作区&#xff0c;编辑模式&#xff0c;全选物体在左边自动展开UV 在物体数据属性-UV贴图-存在默认的UV贴图&#…

华为OD机试真题B卷 Java 实现【输入整型数组和排序标识,对其元素按照升序或降序进行排序】,附详细解题思路

一、题目描述 输入整型数组和排序标识,对其元素按照升序或降序进行排序 数据范围: 1≤n≤1000 ,元素大小满足 0≤val≤100000 。 二、输入描述 第一行输入数组元素个数;第二行输入待排序的数组,每个数用空格隔开;第三行输入一个整数0或1。0代表升序排序,1代表降序排序…

UOS桌面系统使用RLinux恢复数据

UOS桌面系统使用RLinux恢复数据 一、工具介绍二、注意事项三、准备四、制作live系统启动盘五、拷贝文件六、进入live系统一、工具介绍 R-Linux 是一款用于 Linux 和某些 Unixes 操作系统 Ext2/Ext3/Ext4 FS 文件系统的免费文件恢复实用工具。R-Linux 与 R-Studio 使用相同的 I…

如何使用ArcGIS进行选房分析

无论是城市规划布局研究&#xff0c;还是为自己找一个心仪的住房&#xff0c;都需要综合考虑购物、医疗、教育和休闲等诸多因素&#xff0c;若单纯依靠人力去寻找&#xff0c;十分的麻烦和耗时。 此时ArcGIS强大的分析功能就凸显了出来&#xff0c;我们可以通过空间上的距离关…

chatgpt赋能python:Python中同一键可以对应多个值吗?

Python中同一键可以对应多个值吗&#xff1f; Python是一门简单、易学且功能强大的编程语言&#xff0c;它广泛应用于Web开发、机器学习、数据科学等领域。Python的数据结构中的字典&#xff08;dictionary&#xff09;是其中一个非常有用的数据结构&#xff0c;它可以存储键值…

解锁高并发世界:深入探索并发编程和线程池技术的实用指南

《深入理解高并发编程:JDK核心技术》这本书是一本非常实用的编程指南&#xff0c;旨在帮助读者深入理解并发编程和线程池技术。笔者将目录分为两大部分&#xff1a;基础篇、工具篇和线程池技术篇。 这本书提供了广泛的内容覆盖和深入的讲解&#xff0c;适合读者在高并发编程领…

MT8183核心板 MTK8183处理器规格参数

MT8183核心板集成了多项高性能硬件&#xff0c;是一款功耗低、高效能的芯片&#xff0c;可以支持高质量的平板电脑平台设计。该芯片结合了一个八核CPU&#xff0c;其中包括四个Arm Cortex-A73的“大核心”和四个Cortex-A53核心&#xff0c;全部运行速度高达2GHz&#xff0c;还有…

chatgpt赋能python:Python中的//2

Python中的//2 Python是一种广泛使用的动态编程语言&#xff0c;因为它功能强大&#xff0c;易于学习和使用。Python在每个程序员的工具包中占据重要位置&#xff0c;这是因为Python可以用于构建各种应用程序。 本文将讨论Python中的//2运算符&#xff0c;解释其作用和用法&a…

抖音seo源码开发-抖音搜索优化系统-视频批量剪辑系统搭建

抖音seo源码开发&#xff0c;抖音seo开源定制&#xff0c;抖音seo源码交付&#xff0c;抖音seo源码开发是一项重要的技术&#xff0c;可以将您的抖音号排名提升到更高的位置&#xff0c;帮助您吸引更多的关注和粉丝。SEO源码开发需要具备一定的技术和经验&#xff0c;因此建议在…

跨模态检索综述

跨模态检索问题的描述 图1&#xff1a;跨 模 态 检 索 的 形 式 。 跨 模 态 检索 允 许 查 询 样 例 和 候 选 对 象 属 于 不 同 模 态 的 数 据 &#xff0c; 比 如 图 像搜索文本 &#xff0c; 文 本 搜 索 视 频 等 &#xff0c; 这 种 灵 活 多 变 的 检索方 式 能 够 满…

2023 年 PMP 考试难不难?

PMP 真的不难&#xff0c;目前的考试都只有选择题&#xff0c;往后可能会增加别的题型&#xff08;2023,8 月份启用第七版教材&#xff09;&#xff0c; 加入了很多 ACP 敏捷管理的内容&#xff0c;而且 敏捷混合题型占到了 50%。 我从新考纲考完下来&#xff0c;最开始也被折…