SpringBoot获取配置:@Value、@ConfigurationProperties方式

三尺非寒  金牌会员 | 2023-12-8 12:24:05 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 912|帖子 912|积分 2736

配置文件yml
  1. # phantomjs的位置地址
  2. phantomjs:
  3.   binPath:
  4.     windows: binPath-win
  5.     linux: binPath-linux
  6.   jsPath:
  7.     windows: jsPath-win
  8.     linux: jsPath-linux
  9.   imagePath:
  10.     windows: imagePath-win
  11.     linux: imagePath-linux
  12. phantomjs2:
  13.   binPath2: I‘m binPath2
  14.   binPath3: I‘m binPath3
复制代码
一、@Value

1、常规方式


  • 注入(需要把类交给spring)
  1. @Data
  2. @Component
  3. public class PhantomPath {
  4.     @Value("${phantomjs.binPath.windows}")
  5.     private String binPathWin;
  6.     @Value("${phantomjs.jsPath.windows}")
  7.     private String jsPathWin;
  8.     @Value("${phantomjs.binPath.linux}")
  9.     private String binPathLinux;
  10.     @Value("${phantomjs.jsPath.linux}")
  11.     private String jsPathLinux;
  12.     @Value("${phantomjs.imagePath.windows}")
  13.     private String imagePathWin;
  14.     @Value("${phantomjs.imagePath.linux}")
  15.     private String imagePathLinux;
  16.     //下面可以直接在方法中使用
  17. }
复制代码

  • 使用(可以直接在注入的类中使用)
  1.     @Resource
  2.     private PhantomPath phantomPath;
  3.     @Test
  4.     public void test03()throws Exception{
  5.         System.out.println(phantomPath.getBinPathWin());
  6.         System.out.println(phantomPath.getJsPathWin());
  7.         System.out.println(phantomPath.getBinPathLinux());
  8.         System.out.println(phantomPath.getJsPathLinux());
  9.         System.out.println(phantomPath.getImagePathWin());
  10.         System.out.println(phantomPath.getImagePathLinux());
  11.     }
复制代码

  • 测试

2、注入到静态属性上


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

    这样是获取不到值的

  • 注入(需要注入到非静态set方法上,再复制给静态属性)
  1. package com.cc.urlgethtml.utils;
  2. import lombok.Data;
  3. import lombok.Getter;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.stereotype.Component;
  6. /**
  7. * <p>根据不同系统获取不同路径</p>
  8. *
  9. * @author CC
  10. * @since 2023/11/3
  11. */
  12. @Component
  13. public class PhantomPathStatic {
  14.     public static String binPathWin;
  15.     public static String jsPathWin;
  16.     //必须是非静态的set方法
  17.     @Value("${phantomjs.binPath.windows}")
  18.     public void setBinPathWin(String binPathWin) {
  19.         PhantomPathStatic.binPathWin = binPathWin;
  20.     }
  21.     @Value("${phantomjs.jsPath.windows}")
  22.     public void setJsPathWin( String jsPathWin) {
  23.         PhantomPathStatic.jsPathWin = jsPathWin;
  24.     }
  25.     public static String getBinPathWin() {
  26.         return binPathWin;
  27.     }
  28.     public static String getJsPathWin() {
  29.         return jsPathWin;
  30.     }
  31. }
复制代码

  • 使用(有两种方式:静态属性方式、get方式)
  1.     @Resource
  2.     private PhantomPathStatic phantomPathStatic;
  3.     @Test
  4.     public void test04()throws Exception{
  5.         System.out.println(phantomPathStatic.getBinPathWin());
  6.         System.out.println(PhantomPathStatic.binPathWin);
  7.         System.out.println(phantomPathStatic.getJsPathWin());
  8.         System.out.println(PhantomPathStatic.jsPathWin);
  9.     }
复制代码

  • 测试

一、@ConfigurationProperties

1、常规方式


  • 注入
  1. @Data
  2. @Component
  3. @ConfigurationProperties(prefix = "phantomjs2")
  4. public class PhantomConPro {
  5.     private String binPath2;
  6.     private String binPath3;
  7. }
复制代码

  • 使用、测试

2、获取map方式


  • 注入
  1. package com.cc.urlgethtml.utils;
  2. import lombok.Data;
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. import org.springframework.stereotype.Component;
  5. import java.util.Map;
  6. /**
  7. * <p></p>
  8. *
  9. * @author CC
  10. * @since 2023/11/7
  11. */
  12. @Data
  13. @Component
  14. @ConfigurationProperties(prefix = "phantomjs")
  15. public class PhantomConProMap {
  16.     private Map<String, String> binPath;
  17.     private Map<String, String> jsPath;
  18.     private Map<String, String> imagePath;
  19. }
复制代码

  • 使用、测试

3、注入到静态属性上


  • 注入
  1. package com.cc.urlgethtml.utils;
  2. import lombok.Data;
  3. import lombok.Getter;
  4. import org.springframework.boot.context.properties.ConfigurationProperties;
  5. import org.springframework.stereotype.Component;
  6. import java.util.Map;
  7. /**
  8. * <p></p>
  9. *
  10. * @author CC
  11. * @since 2023/11/7
  12. */
  13. @Component
  14. @ConfigurationProperties(prefix = "phantomjs")
  15. public class PhantomConProMapStatic {
  16.     @Getter
  17.     public static Map<String, String> binPath;
  18.     @Getter
  19.     public static Map<String, String> jsPath;
  20.     @Getter
  21.     public static Map<String, String> imagePath;
  22.     //必须是非静态的set方法
  23.     public void setBinPath(Map<String, String> binPath) {
  24.         PhantomConProMapStatic.binPath = binPath;
  25.     }
  26.     public void setJsPath(Map<String, String> jsPath) {
  27.         PhantomConProMapStatic.jsPath = jsPath;
  28.     }
  29.     public void setImagePath(Map<String, String> imagePath) {
  30.         PhantomConProMapStatic.imagePath = imagePath;
  31.     }
  32. }
复制代码

  • 使用、测试(三种使用方式)

三、总结

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

三尺非寒

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表