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

链接: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. Golang Gtk+3教程:Grid布局

    在上个例子中我们使用了box布局,现在让我们来学习另一种布局--grid.其实这几种布局都大同小异,如果你看懂了上一个例子,想必使用grid也不是难事. 程序运行效果: package main im ...

  2. 实现0.5px边框线

    实现0.5px边框方法 方案一:利用渐变(原理:高度1px,背景渐变,一半有颜色,一半透明) CSS部分 .container { width: 500px; margin: 0px auto; } ...

  3. Jmeter 常用函数(8)- 详解 __MD5

    如果你想查看更多 Jmeter 常用函数可以在这篇文章找找哦 https://www.cnblogs.com/poloyy/p/13291704.html 作用 将指定的字符串 MD5 加密并返回,加 ...

  4. Java面试通关要点汇总整理

    简历篇 请自我介绍 请介绍项目 基础篇 基本功 面向对象的特征 final, finally, finalize 的区别 int 和 Integer 有什么区别 重载和重写的区别 抽象类和接口有什么区 ...

  5. get customer attribute option

    Mage::getResourceSingleton('customer/customer')->getAttribute('gender')->getSource()->getAl ...

  6. Magento1.9 add attribute to catalog product & assign to all attribute set general group

    $installer = $this; $attributes = array( 'region' => array( 'type' => 'int', 'input' => 'se ...

  7. golang 判断IPv4 or IPv6 address

    import strings func IsIPv4(address string) bool { return strings.Count(address, ":") < ...

  8. openvswitch常用bond相关命令

    1.列出bondovs-appctl bond/list[root@test~]# ovs-appctl bond/listbond type recircID slavesbond1 balance ...

  9. 强化学习模型实现RL-Adventure

    源代码:https://github.com/higgsfield/RL-Adventure 在Pytorch1.4.0上解决bug后的复现版本:https://github.com/lucifer2 ...

  10. MPI中的cannon算法

    Cannon算法 算法过程 假设矩阵\(A,B\)和\(C\)都可以分成\(m\times m\)块矩阵,即\(A = (A_{(ij)})_{m\times m},B = (B_{(ij)})_{m ...