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

news/2024/10/21 3:41:36/

文章目录

  • 一、使用@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;}@Overridepublic 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;@Testpublic 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")@ResponseBodypublic 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")@ResponseBodypublic 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")@ResponseBodypublic 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;@Testpublic 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;@Testpublic void testCustomService() {customService.welcome();}
}

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

在这里插入图片描述


http://www.ppmy.cn/news/106504.html

相关文章

Flutter的手势识别功能实现GestureDetector

GestureDetector简介 GestureDetector 是 Flutter 中一个非常常用的小部件&#xff0c;它提供了许多手势识别的功能&#xff0c;包括点击、双击、长按、拖动、缩放等等。 使用方法 GestureDetector 可以包裹其他部件&#xff0c;当用户在这些部件上进行手势操作时&#xff0…

一文搞懂激活函数(Sigmoid/ReLU/LeakyReLU/PReLU/ELU)

深度学习算法之前的机器学习算法&#xff0c;并不需要对训练数据作概率统计上的假设&#xff1b;但为了让深度学习算法有更好的性能&#xff0c;需要满足的关键要素之一&#xff0c;就是&#xff1a;网络的输入数据服从特定的分布&#xff1a; 数据分布应该是零均值化的&#…

巧计口诀-软件测试的生命周期,黑盒测试设计方法

又到了找工作的日子&#xff0c;背诵这些基本知识和概念又开始了。我找到一个好办法背诵这些方法&#xff1a; 软件测试的生命周期是“分级设编执评” &#xff0c;这样理解啊&#xff1a;“有个公司啊&#xff0c;要施行分级设计编制&#xff0c;就要执行评估了&#xff0c;大…

好用的自动化框架-Allure

概述 报告主要包含总览、类别、测试套件、图表、时间刻度、功能、包等7大部分&#xff0c;支持自定义诸多信息&#xff0c;包括附件添加、缺陷链接、案例链接、测试步骤、Epic、Feature、Story、Title、案例级别等&#xff0c;相当强大。 allure与pytest的结合使用可以呈现完…

golang 协程的实现原理

核心概念 要理解协程的实现, 首先需要了解go中的三个非常重要的概念, 它们分别是G, M和P, 没有看过golang源代码的可能会对它们感到陌生, 这三项是协程最主要的组成部分, 它们在golang的源代码中无处不在. G (goroutine) G是goroutine的头文字, goroutine可以解释为受管理的…

优化版本 穿越火线(CF) FPS AI 自瞄 代码 权重 数据集(下面有链接)

更新初衷 本人在制作过程中&#xff0c;有一些爱钻研的朋友来问以及提出增加一些新的功能点回会更好&#xff0c;本着学习研究态度&#xff0c;在第一个版本上进行优化&#xff0c;增加一些内容 不喜欢看过程的小伙伴直接看最下面 界面 解决问题&#xff1a; 1、进入慢 2、无…

知识变现海哥:知识付费小程序的10大坑

知识付费小程序十个坑你不要掉进去&#xff0c; D一&#xff0c;小程序付费内容要全 电子书、PPT、音频、视频都要包含&#xff0c;现在很多付费内容只包含视频&#xff0c;这种小程序不能购买的。 D二&#xff0c;付费内容有试学功能 只有通过试用、试学给用户建立信任&…

密码学知识

密码学是网络安全、信息安全、区块链等产品的基础&#xff0c;常见的非对称加密、对称加密、散列函数等&#xff0c;都属于密码学范畴。 明文:加密前的消息叫明文&#xff08;plain text&#xff09; 密文:加密后的文本叫密文&#xff08;cipher text&#xff09; 密钥:只有掌…