spring boot加载properties文件代码示例

ゝ一世哀愁。 2022-05-17 08:38 327阅读 0赞
  1. package com.leju.robot.customer.config;
  2. import com.leju.robot.customer.common.consts.RobotConsts;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.PropertySource;
  8. import javax.annotation.PostConstruct;
  9. /**
  10. * 对外接口相关配置
  11. * Created by Administrator on 2018/8/8.
  12. */
  13. @Configuration//被spring容器管理
  14. @PropertySource("classpath:env.properties")//加载properties的位置,如果不写默认加载的是application.properties
  15. public class EnvConfig {
  16. private static final Logger logger = LoggerFactory.getLogger(EnvConfig.class);
  17. @Value("${zygw.query.list.url}")//使用@value注解获取properties中的文件
  18. private String zygwUrl;
  19. @Value("${contentpool.houses.url}")
  20. private String contentPoolHousesUrl;
  21. @Value("${contentpool.houses.type}")
  22. private String contentPoolHousesType;
  23. @Value("${contentpool.houses.key}")
  24. private String contentPoolHousesKey;
  25. @Value("${contentpool.houses.appid}")
  26. private String contentPoolHousesAppid;
  27. @PostConstruct//在servlet初始化的时候加载,并且只加载一次,和构造代码块的作用类似
  28. private void init(){
  29. logger.info("load env.properties start!");
  30. RobotConsts.API_ZYGW_URL = zygwUrl;
  31. RobotConsts.API_CONTENT_HOUSES_URL = contentPoolHousesUrl;
  32. RobotConsts.CONTENT_HOUSES_TYPE = contentPoolHousesType;
  33. RobotConsts.CONTENT_HOUSES_KEY = contentPoolHousesKey;
  34. RobotConsts.CONTENT_HOUSES_APPID = contentPoolHousesAppid;
  35. logger.info("load env.properties end!");
  36. }
  37. }
  38. package com.leju.robot.customer.common.consts;
  39. /**
  40. * Created by Administrator on 2018/7/26.
  41. */
  42. public class RobotConsts {
  43. /**
  44. * 根据城市id和楼盘id获取置业顾问列表的接口地址
  45. */
  46. public static String API_ZYGW_URL = "";
  47. /**
  48. * 内容池-楼盘库 搜索接口地址
  49. */
  50. public static String API_CONTENT_HOUSES_URL = "";
  51. /**
  52. * 内容池-楼盘库 查询类型
  53. */
  54. public static String CONTENT_HOUSES_TYPE = "";
  55. /**
  56. * 内容池-楼盘库 向内容池申请的对应业务Key
  57. */
  58. public static String CONTENT_HOUSES_KEY = "";
  59. /**
  60. * 内容池-楼盘库 向内容池申请的对应业务的APPID
  61. */
  62. public static String CONTENT_HOUSES_APPID = "";
  63. }

70

发表评论

表情:
评论列表 (有 0 条评论,327人围观)

还没有评论,来说两句吧...

相关阅读