Java读取配置文件的几种方法

一、读取xml配置文件(一)新建一个java beanpackage chb.demo.vo;public class HelloBean {private String helloWorld;public String getHelloWorld() {return helloWorld;}public void setHelloWorld(String helloWorld) {this.helloWorld = helloWorld;}}(二)构造一个配置文件?xml version="1.0" encodi...
Java读取配置文件的几种方法
在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置文件来完成,本文根据笔者工作中用到的读取配置文件的方法小小总结一下,主要叙述的是spring读取配置文件的方法。一、读取xml配置文件(一)新建一个java beanpackage chb.demo.vo;public class HelloBean {private String helloWorld;public String getHelloWorld() {return helloWorld;}public void setHelloWorld(String helloWorld) {this.helloWorld = helloWorld;}}(二)构造一个配置文件?xml version="1.0" encoding="UTF-8"?!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ""beansbean id="helloBean" class="chb.demo.vo.HelloBean"property name="helloWorld"valueHello!chb!/value/property/bean/beans(三)读取xml文件1.利用ClassPathXmlApplicationContextApplicationContext context = new ClassPathXmlApplicationContext("beanConfig.xml");HelloBean helloBean = (HelloBean)context.getBean("helloBean");System.out.println(helloBean.getHelloWorld());2.利用FileSystemResource读取Resource rs = new FileSystemResource("D:/software/tomcat/webapps/springWebDemo/WEB-INF/classes/beanConfig.xml");BeanFactory factory = new XmlBeanFactory(rs);HelloBean helloBean = (HelloBean)factory.getBean("helloBean");System.out.println(helloBean.getHelloWorld());值得注意的是:利用FileSystemResource,则配置文件必须放在project直接目录下,或者写明绝对路径,否则就会抛出找不到文件的异常。二、读取properties配置文件这里介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取(一)利用spring读取properties 文件我们还利用上面的HelloBean.java文件,构造如下beanConfig.properties文件:helloBean.class=chb.demo.vo.HelloBeanhelloBean.helloWorld=Hello!chb!属性文件中的"helloBean"名称即是Bean的别名设定,.class用于指定类来源。然后利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件BeanDefinitionRegistry reg = new DefaultListableBeanFactory();PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(reg);reader.loadBeanDefinitions(new ClassPathResource("beanConfig.properties"));BeanFactory factory = (BeanFactory)reg;HelloBean helloBean = (HelloBean)factory.getBean("helloBean");System.out.println(helloBean.getHelloWorld());(二)利用java.util.Properties读取属性文件比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,如:ip=192.168.0.1port=8080则,我们可以用如下程序来获得服务器配置信息:InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ipConfig.properties");Properties p = new Properties();try {p.load(inputStream);} catch (IOException e1) {e1.printStackTrace();}System.out.println("ip:"+p.getProperty("ip")+",port:"+p.getProperty("port"));2023-04-28
mengvlog 阅读 8 次 更新于 2025-07-19 23:49:48 我来答关注问题0
  •  翡希信息咨询 java读取配置文件的几种方法

    在Java中读取配置文件主要有以下几种方法:1. 采用ServletContext读取 方法描述:通过ServletContext读取配置文件的realpath,然后使用文件流将其读取出来。配置文件可以存放在webinfo的classes目录中,或在应用层级及webinfo的目录中。 适用场景:适用于Web应用,且配置文件可以放在src、webinfo及webroot等目录下。

  •  深空见闻 springboot中java读取yml配置文件 打包后读取

    在Spring Boot中,Java读取yml配置文件并在打包后依然能够读取的方法主要有以下几种:使用@Value注解:简介:这是最简单和直接的方法,适用于读取单个配置项。实现:在需要读取配置的字段上使用@Value注解,并指定配置项的key。例如:@Value("${student.name:'aaa'}") private String name;。

  •  阿暄生活 你会几种读取/加载 properties配置文件方法

    在Java项目中,读取/加载properties配置文件的方法主要有以下几种:基于ClassLoader的读取方法:描述:这是一种在类路径下操作的便捷方式。特点:局限性在于只能在类路径范围内使用。通过InputStream读取:描述:可以直接读取文件内容,实现对配置文件的定制操作。特点:适用于需要更多自定义处理的场景。使用Resou...

  •  深空见闻 springboot中java读取yml配置文件 打包后读取

    默认加载内部配置文件Spring Boot 打包后会默认读取 jar 包内 src/main/resources 目录下的 application.yml(或 application.properties)。若配置文件路径正确,无需额外操作即可直接读取。外部配置文件加载通过命令行参数指定外部 YML 文件路径,优先级高于内部配置,适合生产环境动态调整。使用命令 java -jar...

  •  文暄生活科普 你会几种读取/加载 properties配置文件方法

    接下来,更灵活的方式是通过InputStream,你可以直接读取文件内容,实现对配置文件的定制操作。这种方法适用于需要更多自定义处理的场景。ResourceBundle也是一种实用的选择,它将配置文件组织成易于管理的资源,可以简化代码并提高可维护性。不过,这种方法需要引入commons-configuration2.x包,版本为2,而非1,...

檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部