SpringBoot获取配置:@Value、@ConfigurationProperties方式
配置文件yml
# phantomjs的位置地址
phantomjs:
binPath:
windows: binPath-win
linux: binPath-linux
jsPath:
windows: jsPath-win
linux: jsPath-linux
imagePath:
windows: imagePath-win
linux: imagePath-linux
phantomjs2:
binPath2: I‘m binPath2
binPath3: I‘m binPath3
一、@Value
1、常规方式
- 注入(需要把类交给spring)
@Data
@Component
public class PhantomPath {
@Value("${phantomjs.binPath.windows}")
private String binPathWin;
@Value("${phantomjs.jsPath.windows}")
private String jsPathWin;
@Value("${phantomjs.binPath.linux}")
private String binPathLinux;
@Value("${phantomjs.jsPath.linux}")
private String jsPathLinux;
@Value("${phantomjs.imagePath.windows}")
private String imagePathWin;
@Value("${phantomjs.imagePath.linux}")
private String imagePathLinux;
//下面可以直接在方法中使用
}
- 使用(可以直接在注入的类中使用)
@Resource
private PhantomPath phantomPath;
@Test
public void test03()throws Exception{
System.out.println(phantomPath.getBinPathWin());
System.out.println(phantomPath.getJsPathWin());
System.out.println(phantomPath.getBinPathLinux());
System.out.println(phantomPath.getJsPathLinux());
System.out.println(phantomPath.getImagePathWin());
System.out.println(phantomPath.getImagePathLinux());
}
- 测试

2、注入到静态属性上
解释
不能这样直接注入到静态属性上

这样是获取不到值的

注入(需要注入到非静态set方法上,再复制给静态属性)
package com.cc.urlgethtml.utils;
import lombok.Data;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* <p>根据不同系统获取不同路径</p>
*
* @author CC
* @since 2023/11/3
*/
@Component
public class PhantomPathStatic {
public static String binPathWin;
public static String jsPathWin;
//必须是非静态的set方法
@Value("${phantomjs.binPath.windows}")
public void setBinPathWin(String binPathWin) {
PhantomPathStatic.binPathWin = binPathWin;
}
@Value("${phantomjs.jsPath.windows}")
public void setJsPathWin( String jsPathWin) {
PhantomPathStatic.jsPathWin = jsPathWin;
}
public static String getBinPathWin() {
return binPathWin;
}
public static String getJsPathWin() {
return jsPathWin;
}
}
- 使用(有两种方式:静态属性方式、get方式)
@Resource
private PhantomPathStatic phantomPathStatic;
@Test
public void test04()throws Exception{
System.out.println(phantomPathStatic.getBinPathWin());
System.out.println(PhantomPathStatic.binPathWin);
System.out.println(phantomPathStatic.getJsPathWin());
System.out.println(PhantomPathStatic.jsPathWin);
}
- 测试

一、@ConfigurationProperties
1、常规方式
- 注入
@Data
@Component
@ConfigurationProperties(prefix = "phantomjs2")
public class PhantomConPro {
private String binPath2;
private String binPath3;
}
- 使用、测试

2、获取map方式
- 注入
package com.cc.urlgethtml.utils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* <p></p>
*
* @author CC
* @since 2023/11/7
*/
@Data
@Component
@ConfigurationProperties(prefix = "phantomjs")
public class PhantomConProMap {
private Map<String, String> binPath;
private Map<String, String> jsPath;
private Map<String, String> imagePath;
}
- 使用、测试

3、注入到静态属性上
- 注入
package com.cc.urlgethtml.utils;
import lombok.Data;
import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* <p></p>
*
* @author CC
* @since 2023/11/7
*/
@Component
@ConfigurationProperties(prefix = "phantomjs")
public class PhantomConProMapStatic {
@Getter
public static Map<String, String> binPath;
@Getter
public static Map<String, String> jsPath;
@Getter
public static Map<String, String> imagePath;
//必须是非静态的set方法
public void setBinPath(Map<String, String> binPath) {
PhantomConProMapStatic.binPath = binPath;
}
public void setJsPath(Map<String, String> jsPath) {
PhantomConProMapStatic.jsPath = jsPath;
}
public void setImagePath(Map<String, String> imagePath) {
PhantomConProMapStatic.imagePath = imagePath;
}
}
- 使用、测试(三种使用方式)

三、总结
参考:https://zhuanlan.zhihu.com/p/639448969
SpringBoot获取配置:@Value、@ConfigurationProperties方式的更多相关文章
- SpringBoot自定义属性配置以及@ConfigurationProperties注解与@Value注解区别
我们可以在application.properties中配置自定义的属性值,为了获取这些值,我们可以使用spring提供的@value注解,还可以使用springboot提供的@Configurati ...
- SpringBoot 获取配置 @Value
@Value注解可以在代码中直接取到相应的值 如在application.yml中 # 自定义属性 leysen: xcx: url: aaa 1.java代码里的属性值是非静态的,直接在属性上加@V ...
- SpringBoot配置分析、获取到SpringBoot配置文件信息以及几种获取配置文件信息的方式
Spring入门篇:https://www.cnblogs.com/biehongli/p/10170241.html SpringBoot的默认的配置文件application.properties ...
- SpringBoot获取全局配置文件的属性以及@ConfigurationProperties实现类型安全的配置
在SpringBoot,可以定义一个全局配置文件,全局配置文件有两种形式: 1). application.properties 2).application.yml 二者的后缀名不同,编辑的格式也不 ...
- 掌握这些springboot的配置方式,让你工作效率翻个倍!
springboot的多种配置方式 java配置主要靠java类和一些注解,比较常用的注解有: @Configuration :声明一个类作为配置类,代替xml文件 @Bean :声明在方法上,将方法 ...
- SpringBoot三种配置Dubbo的方式
*必须首先导入dubbo-starter (1).使用SpringBoot配置文件(application.properties或application.yml) dubbo.application. ...
- springboot 获取控制器参数的几种方式
这里介绍springboot 获取控制器参数有四种方式 1.无注解下获取参数 2.使用@RequestParam获取参数 3.传递数组 4.通过URL传递参数 无注解下获取参数无注解下获取参数,需要控 ...
- SpringBoot Logback无法获取配置中心属性
SpringBoot Logback无法获取配置中心属性 前言 最近在做项目中,需要把项目中的日志信息通过RabbitMQ将规定格式的消息发送到消息队列中,然后ELK系统通过消息队列拿日志并且保存起来 ...
- 【坑】Mybatis原始获取配置方式,获取配置失败
错误环境: mysql版本:6.0.6 mybatis 3.4.1 idea 2017.1.2 maven 3.5.0 错误描述: 配置经路径见图1,classpath是java文件夹 获取配置的代码 ...
- 补习系列(10)-springboot 之配置读取
目录 简介 一.配置样例 二.如何注入配置 1. 缺省配置文件 2. 使用注解 3. 启动参数 还有.. 三.如何读取配置 @Value 注解 Environment 接口 @Configuratio ...
随机推荐
- top 命令解释
PID:进程ID USER:运行改进程的用户 PR:进程的优先级 NI:Nice值,进程的优先级修正值,负值表示高优先级,正值表示低优先级 VIRT:虚拟内存,进程使用的虚拟内存总量 RES:物理内存 ...
- wordpress自建博客站,在页脚添加网站总运行时间
wordpress自建博客站,在页脚添加网站总运行时间 笔者使用的主题是 GeneratePress 版本:3.1.3 <span id="momk" style=" ...
- #分类讨论#CF891A Pride
题目 你有一个长度为 \(n\) 的数列 \(a\),你能执行一些操作. 每个操作是这样的:选择两个相邻的数 \(x\) 和 \(y\),把 它们中的一个 换为 \(\gcd(x,y)\). 问你把数 ...
- 中文GPTS详尽教程,字节扣子Coze插件使用全输出
今天,斜杠君和大家分享如何在字节扣子Coze中创建插件,并在创建后如何使用这个插件. 一.新建插件 首先,进入到插件页面,创建一个插件. https://www.coze.cn/home 点击左侧的个 ...
- C#中yield return的作用
C#中yield return的作用 yield return作用在 return 时,保存当前函数的状态,下次调用时继续从当前位置处理.示例说明如下代码所示,主函数使用 foreach 输出 Get ...
- react native 0.73 配置 react-native-fs
安装react-native-fs npm npm install react-native-fs --save yarn yarn add react-native-fs 安卓配置 android/ ...
- HarmonyOS 自定义页面请求与前端页面调试
一.自定义页面请求响应 Web组件支持在应用拦截到页面请求后自定义响应请求能力.开发者通过onInterceptRequest()接口来实现自定义资源请求响应 .自定义请求能力可以用于开发者自定义 ...
- 【直播回顾】Hello HarmonyOS进阶课程第五课——原子化服务
由HDE李洋老师主讲的Hello HarmonyOS进阶系列应用篇第五课<原子化服务>, 已于6月1日晚上 19 点在HarmonyOS社群内成功举行.本节课李洋老师带领大家了解Harmo ...
- Hadoop之Hive架构与设计
Hadoop之Hive架构与设计 Hadoop是一个能够对大量数据进行分布式处理的软件框架.具有可靠.高效.可伸缩的特点. HDFS:全称为Hadoop分布式文件系统(Hadoop Distribut ...
- python实现:有一个列表为num_list,找到一个具有最大和的连续子列表,返回其最大和。
# 有一个列表为num_list,找到一个具有最大和的连续子列表,返回其最大和.# 示例:# 输入: [-3,1,-1,6,-1,2,4,-5,4]# 输出: 11# 解释: 连续子数组 [6,-1, ...