前一篇博客实现了打开第一个页面

链接:https://blog.csdn.net/qq_38175040/article/details/105709758

本篇博客实现在框架中注入properties文件中的值

首先在resource下创建一个book.properties文件,如下:

然后创建一个bookbean.java文件,如下,我把代码贴一下:

package com.example.demo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "book")
@PropertySource("classpath:book.properties")
public class BookBean {
private String name;
private String author;
private String price; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
} public String getPrice() {
return price;
} public void setPrice(String price) {
this.price = price;
}
}

这里要注意 @ConfigurationProperties的写法
旧版本的写法是:@ConfigurationProperties(prefix = “book”,locations = “classpath:book.properties”)
现在我们这样写:
@ConfigurationProperties(prefix = “book”)
@PropertySource(“classpath:book.properties”)

最后一步

DemoApplication.java(每个人命名的可能不一样) 中添加如下内容:

	@Autowired
private BookBean bookBean;
@RequestMapping("/book")
public String book(){
return "Hello Spring Boot! The BookName is "+bookBean.getName()+";and Book Author is "+bookBean.getAuthor()+";and Book price is "+bookBean.getPrice();
}

好了,现在打开页面看看效果:

更多细节可以参考:https://blog.csdn.net/u012702547/article/details/53740047

ok,这不就行了吗,要的就是这个效果

spring boot 在框架中注入properties文件里的值(Spring三)的更多相关文章

  1. Spring boot 梳理 - SpringBoot中注入ApplicationContext对象的三种方式

    直接注入(Autowired) @Configuration public class OAConfig { @Autowired private ApplicationContext applica ...

  2. spring boot:接收数组参数及多文件混合json参数(spring boot 2.3.4)

    一,生产环境中的复杂参数上传的场景 1,保存排序值 : 例如:某一件商品的多张展示图片排序,提交的排序值要和图片的id相对应 2,上传多张图片,图片要和指定的变量相对应 例如:在添加商品sku时, 需 ...

  3. spring注解中使用properties文件

    一.只读取单个 properties 文件 1.在 spring 的配置文件中,加入 引入命名空间: xmlns:util="http://www.springframework.org/s ...

  4. 谈谈Spring 注入properties文件总结

    本篇谈谈Spring 注入properties文件总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 spring提供了多种方式来注入properties文件,本文做一个 ...

  5. Spring MVC 中使用properties文件

    首先要搭建Spring mvc的环境,然后开始properties文件里的配置: 第一步:在springcontext中注入properties,具体路径自己调整 <bean id=" ...

  6. spring boot 学习(六)spring boot 各版本中使用 log4j2 记录日志

    spring boot 各版本中使用 log4j2 记录日志 前言 Spring Boot中默认日志工具是 logback,只不过我不太喜欢 logback.为了更好支持 spring boot 框架 ...

  7. 五种方式让你在java中读取properties文件内容不再是难题

    一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...

  8. Spring boot 国际化自动加载资源文件问题

    Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义 ...

  9. Spring Boot通过ImportBeanDefinitionRegistrar动态注入Bean

    在阅读Spring Boot源码时,看到Spring Boot中大量使用ImportBeanDefinitionRegistrar来实现Bean的动态注入.它是Spring中一个强大的扩展接口.本篇文 ...

随机推荐

  1. 如何实现字符串转换成整数(实现atoi内置函数)?

    题目描述 输入一个由数字组成的字符串,把它转换成整数并输出.例如:输入字符串"123",输出整数123. 给定函数原型int StrToInt(const char *str) , ...

  2. Android小技巧总结——持续更新

    WebView实现 博客地址: https://blog.csdn.net/lowprofile_coding/article/details/77928614 获取网络权限 <uses-per ...

  3. (数据科学学习手札93)利用geopandas与PostGIS进行交互

    本文完整代码及数据已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 PostGIS作为postgresql针对 ...

  4. Python+Pytest+Allure+Git+Jenkins接口自动化框架

    Python+Pytest+Allure+Git+Jenkins接口自动化框架 一.接口基础 接口测试是对系统和组件之间的接口进行测试,主要是效验数据的交换,传递和控制管理过程,以及相互逻辑依赖关系. ...

  5. clients-producer-组包发送消息

  6. Socket、ServerSocket

    1.服务器端程序 package demo12.net; import java.io.IOException; import java.io.InputStream; import java.io. ...

  7. css 引入的 方式有哪些? link与post有什么区别??

    有四钟形式: 1.链入外部样式表,就是把样式表保存为一个样式表文件,然后在页面中用<link rel="stylesheet" type="text/css&quo ...

  8. Hitool打开出现failed to create the java virtual machine

    今天在安装Hitool后,打开hitool后出现了错误:failed to create the java virtual machine. 解决方法如下: 记事本打开HiTool.ini -star ...

  9. Go interface 操作示例

    原文链接:Go interface操作示例 特点: 1. interface 是一种类型 interface 是一种具有一组方法的类型,这些方法定义了 interface 的行为.go 允许不带任何方 ...

  10. 新建一个Vue项目

    node环境以及vue的安装可查看:https://www.cnblogs.com/renlywen/p/13522869.html 第一步:创建项目 终端输入: vue init webpack d ...