SpringBoot中读取application.properties配置文件的方法
作者:Knight_AL 发布时间:2023-10-20 17:29:05
标签:SpringBoot,application,properties
application.properties有以下这几条数据
方法一:@Value注解+@Component
建议properties少的时候用,多的时候就不要使用这种方法了
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Value("${wx.open.app_id}")
private String appid;
@Value("${wx.open.app_secret}")
private String secret;
@Value("${wx.open.redirect_url}")
private String url;
@RequestMapping("hello")
public String test(){
return appid+"---"+secret+"---"+url;
}
}
另一种方法
创建一个WeProperties
@Component
@Data
public class WeProperties {
@Value("${wx.open.app_id}")
private String appid;
@Value("${wx.open.app_secret}")
private String secret;
@Value("${wx.open.redirect_url}")
private String url;
}
Controller层
@RestController
public class UserController {
@Autowired
private WeProperties properties;
@RequestMapping("hello")
public String test(){
return properties.getAppid()+"---"+properties.getSecret()+"---"+properties.getUrl();
}
}
方法二:@Component+@ConfigurationProperties
创建一个WeProperties
后面的属性名一定要保持一致
@Component
@ConfigurationProperties(prefix = "wx.open")
@Data
public class WeProperties {
private String appid;
private String app_secret;
private String redirect_url;
}
Controller层
@RestController
public class UserController {
@Autowired
private WeProperties properties;
@RequestMapping("hello")
public String test(){
return properties.getAppid()+"---"+properties.getApp_secret()+"---"+properties.getRedirect_url();
}
}
方法三:@ConfigurationProperties+@EnableConfigurationProperties
创建一个WeProperties
后面的属性名一定要保持一致
@ConfigurationProperties(prefix = "wx.open")
@Data
public class WeProperties {
private String appid;
private String app_secret;
private String redirect_url;
}
启动类添加@EnableConfigurationProperties
@SpringBootApplication
@EnableConfigurationProperties(value = WeProperties.class)
public class PropertiesApplication {
public static void main(String[] args) {
SpringApplication.run(PropertiesApplication.class,args);
}
}
Controller层
@RestController
public class UserController {
@Autowired
private WeProperties properties;
@RequestMapping("hello")
public String test(){
return properties.getAppid()+"---"+properties.getApp_secret()+"---"+properties.getRedirect_url();
}
}
来源:https://blog.csdn.net/qq_46548855/article/details/128892215


猜你喜欢
- 工具方法:本文的目的是把json串转成map键值对存储,而且只存储叶节点的数据maven 引用jar包版本:<dependency&g
- 现在软件或者网页的并发量越来越大了,大量请求直接操作数据库会对数据库造成很大的压力,处理大量连接和请求就会需要很长时间,但是实际中百分之80
- 1.前言string是属于引用类型的,这个大家都知道吧?但是平常在使用的过程中,发现它还是拥有一些值类型的特征的,这到底是为什么呢?原因就是
- 本文介绍了Android EasyBarrage实现轻量级弹幕效果,分享给大家,具体如下:概述EasyBarrage是Android平台的一
- 话不多说,请看代码:public FileResult GetExcelFile() {  
- Vibrator振动器,是手机自带的振动器哦,不要想成岛国用的那种神秘东西哦~~Vibrator是Android给我们提供的用于机身震动的一
- 1. Dubbo是什么?Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,
- 二维数组遍历:思想:1.先将二维数组中所有的元素拿到2.再将二维数组中每个元素进行遍历,相当于就是在遍历一个一维数组第一种方法:双重for循
- 如今代码圈很多做网络爬虫的例子,今天小编给大家分享的是如何用C#做网络爬虫。注意这次的分享只是分享思路,并不是一整个例子,因为如果要讲解一整
- 目录1、简单介绍2、Lambdas和Scopes3、Lambdas与局部变量4、Lambda体与局部变量5、Lambdas和'Thi
- 本文实例讲述了C#实现的微信网页授权操作逻辑封装。分享给大家供大家参考,具体如下:一、微信网页授权登录前提:1.已经获取的接口权限,如果是测
- package com.yswc.dao.sign;import java.io.BufferedReader;import java.io
- 本文实例讲述了C#实现基于XML配置MenuStrip菜单的方法。分享给大家供大家参考。具体如下:1.关于本程序的说明用XML配置MenuS
- 根据poi接收controller层的excel文件导入 可使用后缀
- 线程间通信:由于多线程共享地址空间和数据空间,所以多个线程间的通信是一个线程的数据可以直接提供给其他线程使用,而不必通过操作系统(也就是内核
- RocketMQ 是什么Github 上关于 RocketMQ 的介绍:RcoketMQ 是一款低延迟、高可靠、可伸缩、易于使用的消息中间件
- 很久以来都想研究一下利用java操作Excel的方法,今天没事,就稍微了解了一下,特总结一下。利用java操作Excel,有个开源的东东-j
- 在讲使用path绘制多边形时,讲下Canvas的translate(),rotate()方法的使用,本博客中会使用这方面的知识,先单独讲下,
- 本实例是为了实现在管理后台实现微信菜单的添加删除管理。1、首先我们需要新建一个数据库表用于存放menu菜单项可包含的字段有id、父类id、n
- 呼吸按钮是我最早接触到为view添加动画效果的需求,刚刚参加安卓开发工作,要求设计一个好看的语音按钮效果,就有了这个成果,但是后来又改方案了