spring boot 在框架中注入properties文件里的值(Spring三)
2024-08-30 01:48:13
前一篇博客实现了打开第一个页面
链接: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,这不就行了吗,要的就是这个效果
最新文章
- mybatis if判断中的特殊符号
- js web实现移动端触控
- 如何在word里面插入目录
- AFNetworking、MKNetworkKit和ASIHTTPRequest对比
- 局域网yum服务器创建
- 带参数的查询防止SQL注入攻击
- 初学JavaScript须知道的七件事
- android TDD平台插入双卡时,查看允许返回发送报告的选项,去掉勾选,不起作用
- MySQL show master / slave status 命令参数
- Bat脚本命令说明
- 基于hadoop分析,了解hive的使用
- datatable处理gridview筛选后的值
- struct 对齐和补齐原则
- Linux下使用ps命令查看某个进程文件的启动位置
- React学习及实例开发(一)——开始(转载)
- SQL取最大值编码(自动编码)
- EF t4模板将实体与DBContext分离
- Maven settings.xml配置(指定本地仓库、阿里云镜像设置)
- Linux学习---linux的svn的配置与安装
- Update submitted Perforce changelist description by P4.net api
热门文章
- 聊聊Java内省Introspector
- SpringBoot实现本地存储文件上传及提供HTTP访问服务
- Solon Ioc 的注解对比Spring及JSR330
- Python实现电脑控制,这个库让你可以控制和监控输入设备
- Hive中的用户自定义函数
- getting session bus failed: //bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed.
- kubeadm安装kubernetes(v18.8.8)
- powerdesigner16.5 生成sql语句
- 精讲响应式WebClient第5篇-请求超时设置与异常处理
- Mysql锁【转】