三尺非寒 发表于 2023-12-8 12:24:05

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());
    }

[*]测试
https://img2023.cnblogs.com/blog/1848807/202311/1848807-20231107101502207-1973555770.png
2、注入到静态属性上


[*]解释
不能这样直接注入到静态属性上
https://img2023.cnblogs.com/blog/1848807/202311/1848807-20231107101847558-593051542.png
这样是获取不到值的
https://img2023.cnblogs.com/blog/1848807/202311/1848807-20231107102130410-172585505.png
[*]注入(需要注入到非静态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);
    }

[*]测试
https://img2023.cnblogs.com/blog/1848807/202311/1848807-20231107103425712-1848629189.png
一、@ConfigurationProperties

1、常规方式


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

    private String binPath2;

    private String binPath3;

}

[*]使用、测试
https://img2023.cnblogs.com/blog/1848807/202311/1848807-20231107110650896-941509700.png
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;

}

[*]使用、测试
https://img2023.cnblogs.com/blog/1848807/202311/1848807-20231107111227327-66567365.png
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://img2023.cnblogs.com/blog/1848807/202311/1848807-20231107112559195-663917960.png
三、总结

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: SpringBoot获取配置:@Value、@ConfigurationProperties方式