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
0
投稿
猜你喜欢
- 引言前边两章说了点基础的,从这章开始,我们挖挖源码。看看RocketMQ是怎么工作的。首先呢,这个生产者就是送孩子去码头的家长,孩子们呢,就
- java.util.concurrent包中的工具实现核心都是AQS,了解ReentrantLock的实现原理,需要先分析AQS以及AQS与
- 本文实例为大家分享了Java实现 * 系统的具体代码,供大家参考,具体内容如下父类Vehiclepublic abstract class
- 已知一副扑克牌有54张,去除大王和小王,剩余52张。在其中随机抽取4张牌,利用加减乘除进行计算得到24. 从A到10,他们的值分别为1到10
- 本文实例为大家分享了java转树形结构工具类的具体代码,供大家参考,具体内容如下import com.alibaba.fastjson.JS
- Spring Security简介Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的
- 一、返回BufferedImage由于spring mvc不支持返回BufferedImage ,所以增加图片转换器@Configurati
- 为什么要自定义缓存注解?Spring Cache本身提供@Cacheable、@CacheEvict、@CachePut等缓存注解,为什么还
- 一、@RequestMapping@RequestMapping注解的源码:@Target({ElementType.TYPE, Eleme
- MultipartResolver和ServletFileUpload冲突如果同时使用了MultipartResolver 和Servlet
- 最近要做一个网站,要求实现验证码程序,经过不断调试,终于成功实现功能。一、验证码生成类生成验证码的话需要用到java的Graphics类库,
- 一、Stream流简单示例需求:按照要求集合创建和遍历创建一个结合,存储多个字符串元素把集合中所有以"张"开头的元素存储
- 启动第二个服务时就会报如下的错误:Server failed to start for port 8080: Address already
- java.math.BigDecimal及加减乘除计算BigDecimal简介BigDecimal用来对需要更大或更小的数进行任意精度的运算
- 【前言】面向资源的 Restful 风格的 api 接口本着简洁,资源,便于扩展,便于理解等等各项优势,在如今的系统服务中越来越受欢迎。.n
- 学过Spring的小伙伴对于IOC一定不陌生,IOC:控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向
- 本文实例为大家分享了Java开发实现人机猜拳游戏的具体代码,供大家参考,具体内容如下猜拳游戏游戏规则:人和电分别出剪刀、石头、布,直到人战胜
- Java List转换成String数组实现代码:List<String> list = new ArrayList<St
- 目标效果: 点击动画按钮之后每张牌各自旋转 散开到屏幕上半部分的任意位置之后回到初始位置 比较像LOL男刀的技能动画 : )1: 创建卡牌对
- 实现“摇一摇”功能,其实很简单,就是检测手机的重力感应,具体实现代码如下:1、在 AndroidManifest.xml 中添加操作权限2、