配置文件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. vue中elementui组件el-dialog拖拽(已处理边界情况)

    全局注册 Vue.directive("elDialogDrag", (el) => { const header = el.querySelector(".el- ...

  2. C# PaddleOCR 车牌识别

    效果 车牌识别测试地址 http://47.108.88.211/manual/VehPlateTest.html 通用OCR识别测试地址 http://47.108.88.211/manual/OC ...

  3. 安卓day_1

    今天买了安卓学习的相关书籍,在网站上找到了安卓学习的视频,了解了安卓学习的方向.

  4. 'scanf': This function or variable may be unsafe

    'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable depreca ...

  5. 攻防世界 Replace Reverse二星题

    解题过程中,虽然解出来了,但是磕磕绊绊犯了一些错误,记录一下 分析过程 PE查壳 有一个upx壳,最下面给了脱壳提示: upx.exe -d Replace.exe 脱壳结束,丢到IDA里,SHIF+ ...

  6. 动图演示步骤 Vmware安装Centos-7 最小安装/图形化界面及常见错误参考,基础配置推荐

    程序软件工具安装篇 --[Linux](Vmware/Centos-7) 目录 程序软件工具安装篇 --[Linux](Vmware/Centos-7) ①:文件准备工作 虚拟机工具安装文件 系统镜像 ...

  7. OpenHarmony 3.1 Release版本关键特性解析——HDI硬件设备接口介绍

    HDF 驱动框架是 OpenAtom OpenHarmony(简称"OpenHarmony")系统硬件生态开放的基础,为驱动开发者提供了驱动加载.驱动服务管理和驱动消息机制等驱动框 ...

  8. 从模型到部署,教你如何用Python构建机器学习API服务

    本文分享自华为云社区<Python构建机器学习API服务从模型到部署的完整指南>,作者: 柠檬味拥抱. 在当今数据驱动的世界中,机器学习模型在解决各种问题中扮演着重要角色.然而,将这些模型 ...

  9. 学习笔记——Python基础

    字符串索引 str = '我是一名学生' print(str[0]) #输出"我" print(str[-6]) #输出"我" 字符串切片:把数据对象的一部分拿 ...

  10. JS 取Json中对象特定属性值

    解析JSON JSON 数据 var str = '[{"a": "1","b": "2"}, {"a&quo ...