栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring框架读取property属性文件常用5种方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring框架读取property属性文件常用5种方法

1、方式一:通过spring框架 PropertyPlaceholderConfigurer 工具实现


    
    
      classpath:conf/jdbc.properties
    
    
      UTF-8
    
    
  
  
  
  
    
    
    
    
  
  
  
    
    
    
    
  
  
  
    
  
  
  
  jdbc.properties文件:
  database.connection.driver=com.mysql.jdbc.Driver
  database.connection.url=jdbc:mysql:/

    //存取properties配置文件key-value结果
    private Properties props;

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
 throws BeansException {
      super.processProperties(beanFactoryToProcess, props);
      this.props = props;
    }

    public String getProperty(String key) {
      return this.props.getProperty(key);
    }

    public String getProperty(String key, String defaultValue) {
      return this.props.getProperty(key, defaultValue);
    }

    public Object setProperty(String key, String value) {
      return this.props.setProperty(key, value);
    }

  }

  @Controller
  @RequestMapping("/")
  public class TestController {
    @Autowired
    PropertyConfigurer propertyConfigurer;

    @RequestMapping("/index.do")
    public String getIndex(HttpServletRequest request, HttpServletResponse response, Model model) {
      Map map = new HashMap();
      map.put("orderNo", "111");

      String address1 = propertyConfigurer.getProperty("static.picture.address1");
      String resRoot = propertyConfigurer.getProperty("resRoot");
      String envName = propertyConfigurer.getProperty("database.connection.username");
      String keyzjbceshi = propertyConfigurer.getProperty("keyceshi");


      map.put("address1", address1);
      map.put("resRoot", resRoot);
      map.put("envName", envName);
      map.put("keyzjbceshi", keyzjbceshi);

      model.addAllAttributes(map);
      return "index/index.ftl";
    }
  }

4、方式四:通过ClassPathResource类进行属性文件的读取使用

public class ReadPropertiesUtils1 {
    private static final Logger LOGGER = LoggerFactory.getLogger(ReadPropertiesUtils1.class);

    private static Properties props = new Properties();

    static {
      ClassPathResource cpr = new ClassPathResource("conf/ref-system-relation.properties");// 会重新加载spring框架
      try {
 props.load(cpr.getInputStream());
      } catch (IOException exception) {
 LOGGER.error("ReadPropertiesUtils1 IOException", exception);
      }
    }

    private ReadPropertiesUtils1() {

    }

    public static String getValue(String key) {
      return (String) props.get(key);
    }

    public static void main(String[] args) {
      System.out.println("static.picture.address1>>>"+ ReadPropertiesUtils1.getValue("static.picture.address1"));
      System.out.println("static.picture.address2>>>"+ ReadPropertiesUtils1.getValue("static.picture.address2"));
    }

  }

5、方式五:通过ContextClassLoader进行属性文件的读取使用

public class ReadPropertiesUtils2 {
    private static final Logger LOGGER = LoggerFactory.getLogger(ReadPropertiesUtils2.class);

    public static String getValue(String key) {
      Properties properties = new Properties();
      try {
 InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("conf/ref-system-relation.properties");
 properties.load(inputStream);
      } catch (FileNotFoundException e) {
 LOGGER.error("conf/web-sys-relation.properties文件没有找到异常", e);
      } catch (IOException e) {
 LOGGER.error("IOException", e);
      }
      return properties.getProperty(key);
    }

    public static void main(String[] args) {
      System.out.println("static.picture.address1>>>" + ReadPropertiesUtils2.getValue("static.picture.address1"));
      System.out.println("static.picture.address2>>>" + ReadPropertiesUtils2.getValue("static.picture.address2"));
    }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/131426.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号