配置文件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方式的更多相关文章

  1. SpringBoot自定义属性配置以及@ConfigurationProperties注解与@Value注解区别

    我们可以在application.properties中配置自定义的属性值,为了获取这些值,我们可以使用spring提供的@value注解,还可以使用springboot提供的@Configurati ...

  2. SpringBoot 获取配置 @Value

    @Value注解可以在代码中直接取到相应的值 如在application.yml中 # 自定义属性 leysen: xcx: url: aaa 1.java代码里的属性值是非静态的,直接在属性上加@V ...

  3. SpringBoot配置分析、获取到SpringBoot配置文件信息以及几种获取配置文件信息的方式

    Spring入门篇:https://www.cnblogs.com/biehongli/p/10170241.html SpringBoot的默认的配置文件application.properties ...

  4. SpringBoot获取全局配置文件的属性以及@ConfigurationProperties实现类型安全的配置

    在SpringBoot,可以定义一个全局配置文件,全局配置文件有两种形式: 1). application.properties 2).application.yml 二者的后缀名不同,编辑的格式也不 ...

  5. 掌握这些springboot的配置方式,让你工作效率翻个倍!

    springboot的多种配置方式 java配置主要靠java类和一些注解,比较常用的注解有: @Configuration :声明一个类作为配置类,代替xml文件 @Bean :声明在方法上,将方法 ...

  6. SpringBoot三种配置Dubbo的方式

    *必须首先导入dubbo-starter (1).使用SpringBoot配置文件(application.properties或application.yml) dubbo.application. ...

  7. springboot 获取控制器参数的几种方式

    这里介绍springboot 获取控制器参数有四种方式 1.无注解下获取参数 2.使用@RequestParam获取参数 3.传递数组 4.通过URL传递参数 无注解下获取参数无注解下获取参数,需要控 ...

  8. SpringBoot Logback无法获取配置中心属性

    SpringBoot Logback无法获取配置中心属性 前言 最近在做项目中,需要把项目中的日志信息通过RabbitMQ将规定格式的消息发送到消息队列中,然后ELK系统通过消息队列拿日志并且保存起来 ...

  9. 【坑】Mybatis原始获取配置方式,获取配置失败

    错误环境: mysql版本:6.0.6 mybatis 3.4.1 idea 2017.1.2 maven 3.5.0 错误描述: 配置经路径见图1,classpath是java文件夹 获取配置的代码 ...

  10. 补习系列(10)-springboot 之配置读取

    目录 简介 一.配置样例 二.如何注入配置 1. 缺省配置文件 2. 使用注解 3. 启动参数 还有.. 三.如何读取配置 @Value 注解 Environment 接口 @Configuratio ...

随机推荐

  1. 容器镜像加速指南:探索 Kubernetes 缓存最佳实践

    介绍 将容器化应用程序部署到 Kubernetes 集群时,由于从 registry 中提取必要的容器镜像需要时间,因此可能会出现延迟.在应用程序需要横向扩展或处理高速实时数据的情况下,这种延迟尤其容 ...

  2. 参数 ora_input_emptystr_isnull 对于数据存储的影响

    原生的PG 对于 '' 和 null 认为是不同值:空值 和不确定值:而oracle 认为二者都是不确定的值.KingbaseES 为了兼容Oracle,增加了参数ora_input_emptystr ...

  3. 【问题解决1】fatal error: X11/XXXX.h: No such file or directory

    问题现象 编译鸿蒙代码时,报如下类似的错误: 错误1: 错误2: 解决方法 step 1:安装依赖文件 sudo apt-get install apt-file sudo apt-file upda ...

  4. Selenium 八大元素定位方式

    UI自动化测本质无非就是: 定位元素 -> 操作元素 -> 模拟页面动作 -> 断言结果 -> 生成测试报告. 所以我们做UI自动化的第一步就是定位元素,如果连元素都定位不到就 ...

  5. Hadoop_05 使用xsync脚本命令分发,手动配置脚本

    在/usr/local/bin 目录下创建 xsync 文件,向里面添加 1 #!/bin/sh 2 # 获取输入参数个数,如果没有参数,直接退出 3 pcount=$# 4 if((pcount== ...

  6. 数组栈(ArrayStack)

    栈   栈是一种线性结构,相比与数组,栈对应的操作时数组的子集,只能从一端添加元素,也只能从一端取出元素,是一种 后进先出(Last In First Ou,LIFO) 的数据结构. push pop ...

  7. 解析 Go 编程语言数据类型:bool、整数、浮点数和字符串详细介绍

    数据类型 数据类型是编程中的重要概念.数据类型指定了变量值的大小和类型.Go是静态类型的,这意味着一旦变量类型被定义,它只能存储该类型的数据. 基本数据类型 Go 有三种基本数据类型: bool:表示 ...

  8. mupdf实用操作demo,C++操作PDF文件

    前文: 最近有个项目,需要读写PDF,本来想着挺简单的,读写PDF有那么多的库可以使用,唰唰的就完成了. 忘记了我写C++的,还是在国产系统上开发的. 所以一般的东西还不好使,因为项目需要在多个架构的 ...

  9. MogDB/opengauss触发器简介(1)

    MogDB/opengauss 触发器简介(1) 触发器是对应用动作的响应机制,当应用对一个对象发起 DML 操作时,就会产生一个触发事件(Event).如果该对象上拥有该事件对应的触发器,那么就会检 ...

  10. Android Compose 入门,深入底层源码分析

    Android Compose 入门,深入底层源码分析 我是跟着AS官网学习的,但是官方的教程写的不是很详细.官网链接 首先创建一个Compose项目,目录结构是这样: ui -> theme ...