首页 > 其他分享 >SpringBoot获取配置文件-@Value、@ConfigurationProperties方式

SpringBoot获取配置文件-@Value、@ConfigurationProperties方式

时间:2023-11-07 11:34:16浏览次数:30  
标签:String 配置文件 phantomjs Value private import public SpringBoot

配置文件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());
    }
  • 测试

image

2、注入到静态属性上

  • 解释
    不能这样直接注入到静态属性上
    image
    这样是获取不到值的
    image

  • 注入(需要注入到非静态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;

    @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);
    }
  • 测试

image

一、@ConfigurationProperties

1、常规方式

  • 注入
@Data
@Component
@ConfigurationProperties(prefix = "phantomjs2")
public class PhantomConPro {

    private String binPath2;

    private String binPath3;

}
  • 使用、测试

image

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;

}
  • 使用、测试

image

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;

    public void setBinPath(Map<String, String> binPath) {
        PhantomConProMapStatic.binPath = binPath;
    }
    public static void setJsPath(Map<String, String> jsPath) {
        PhantomConProMapStatic.jsPath = jsPath;
    }
    public static void setImagePath(Map<String, String> imagePath) {
        PhantomConProMapStatic.imagePath = imagePath;
    }
}
  • 使用、测试(三种使用方式)

image

三、总结

参考:https://zhuanlan.zhihu.com/p/639448969

标签:String,配置文件,phantomjs,Value,private,import,public,SpringBoot
From: https://www.cnblogs.com/kakarotto-chen/p/17814408.html

相关文章

  • SpringBoot 单元测试
    1、什么是单元测试?单元测试(UnitTesting)是一种软件测试方法,用于验证和确认代码中的各个单元(通常是函数、方法或类)是否按照预期工作。单元测试旨在检测代码中的小部分,以确保其功能的正确性。2、单元测试有哪些好处?在单元测试中使用模拟对象来替代实际的数据库访问操作,不会实际修改数......
  • springboot mybatis-plus 登录接口
    下面是使用SpringBoot和MyBatis-Plus实现登录接口的示例代码:添加依赖在pom.xml文件中添加以下依赖:<dependencies><!--SpringBoot--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</a......
  • springboot+mybatis-plus批量删除
    可以使用Mybatis-Plus提供的deleteBatchIds方法来实现批量删除。示例代码:@AutowiredprivateMybatisPlusMappermybatisPlusMapper;publicvoiddeleteBatch(List<Long>ids){mybatisPlusMapper.deleteBatchIds(ids);}其中,MybatisPlusMapper是你的Mapper接口,继承了BaseMa......
  • 如何在idea中创建一个SpringBoot项目
    在IntelliJIDEA中创建一个SpringBoot项目非常简单。下面是一步一步的指南:打开IntelliJIDEA:启动IntelliJIDEA,确保你已经安装并配置好Java开发环境。创建新项目:如果你在IDEA的欢迎界面,点击"CreateNewProject"。如果你已经有一个项目打开,可以通过选择"File"->"New......
  • Java:SpringBoot实现JDK动态代理和CGLIB动态代理
    (目录)需要代理的对象//接口publicinterfacePayService{voidpay();}//实现publicclassAliPayServiceimplementsPayService{@Overridepublicvoidpay(){System.out.println("AliPayService");}}1.JDK动态代理在JDK动态代......
  • SpringBoot通过自定义注解+反射机制比较两个对象不同的属性值
    publicclassFieldComparisonUtil{/**•直接返回一个新的对象,并且对象的值只有被修改的部分••@paramold•@paramsource•@paramisParent•@paramtarget目标对象•@return/**•@paramold进行属性比较的原始数据•@paramsource进行属性比......
  • Linux脚本:批量启动docker容器、批量启动springboot、批量启动Vuejs
    批量启动springboot#!/bin/bash#检查容器是否已经启动check_container(){sudodockerps|grep"$1">/dev/nullif[$?-ne0];thenecho"$1containerisnotrunning.Starting$1..."sudodockerstart"$1"elseecho......
  • springboot第44集:Kafka集群和Lua脚本
    servers:Kafka服务器的地址。这是Kafka集群的地址,生产者将使用它来发送消息。retries:在消息发送失败时,生产者将尝试重新发送消息的次数。这个属性指定了重试次数。batchSize:指定了生产者在发送消息之前累积的消息大小(以字节为单位)。一次性发送多个消息可以提高性能。linger:指定了生......
  • springboot入门
    两年没写了。。连右下的小人都没了。得开始新一阶段的学习了。先从学习springboot及其前置内容开始学习。然后简单复习一下vue框架。idea在创建maven的springboot工程时自动下了个依赖,尽量选择版本低一点的。。适配java8不容易出问题。一旦出问题了多重建项目就会重新下springboo......
  • SpringBoot 基础知识
    ​#SpringBoot#​‍本文基于SpringBoot2.0最新稳定版2.7.6;目前SpringBoot3.0已经发布,后续会体验新版新特性。官网:SpringBoot‍SpringBoot程序的优点起步依赖(简化依赖配置)自动配置(简化常用工程相关配置)辅助功能(内置服务器,.....)快速上手SpringBoot工程联网......