导航:首页 > 编程语言 > java动态配置文件

java动态配置文件

发布时间:2022-04-18 07:59:55

java 怎么读取配置文件

一.读取xml配置文件
(一)新建一个java bean(HelloBean. java)

java代码
(二)构造一个配置文件(beanConfig.xml)
xml 代码
(三)读取xml文件
1.利用

java代码
2.利用FileSystemResource读取
java代码
二.读取properties配置文件
这里介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取
(一)利用spring读取properties 文件
我们还利用上面的HelloBean. java文件,构造如下beanConfig.properties文件:
properties 代码
helloBean.class=chb.demo.vo.HelloBean
helloBean.helloWorld=Hello!chb!
属性文件中的"helloBean"名称即是Bean的别名设定,.class用于指定类来源。
然后利用org.springframework.beans.factory.support.来读取属性文件

java代码

(二)利用java.util.Properties读取属性文件
比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,如:
properties 代码
ip=192.168.0.1
port=8080
三.读取位于Jar包之外的properties配置文件

下面仅仅是列出读取文件的过程,剩下的解析成为properties的方法同上
1 FileInputStream reader = new FileInputStream("config.properties");

2 num = reader.read(byteStream);

3 ByteArrayInputStream inStream = new ByteArrayInputStream(byteStream, 0, num);

四.要读取的配置文件和类文件一起打包到一个Jar中
String currentJarPath = URLDecoder.decode(YourClassName.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8"); //获取当前Jar文件名,并对其解码,防止出现中文乱码
JarFile currentJar = new JarFile(currentJarPath);
JarEntry dbEntry = currentJar.getJarEntry("包名/配置文件");
InputStream in = currentJar.getInputStream(dbEntry);
//以上YourClassName是class全名,也就是包括包名

修改:
JarOutputStream out = new FileOutputStream(currentJarPath);
out.putNextEntry(dbEntry);
out.write(byte[] b, int off, int len); //写配置文件
。。。

out.close();

⑵ 怎样使用配置文件设置Java系统属性

右击桌面上“计算机”图标,从弹出的快捷菜单中选择“属性”,打开“系统”窗口。
配置Java的系统环境变量
在打开的“系统”窗口中单击左侧“高级系统设置”选项。
配置Java的系统环境变量
在弹出的“系统属性”对话框中,切换到“高级”选项卡,单击“环境变量”按钮。
配置Java的系统环境变量
4
在弹出的“环境变量”对话框中,在“系统变量”栏中选择“Path”选项,然后单击“编辑”按钮。
配置Java的系统环境变量

⑶ java 可以使用动态参数配置persistence.xml吗

当然可以,要不然怎么叫动态配置呢
如果想实现动态配置,那么每次调用的时候都需要重新load一下
InputStream is = new FileInputStream(file);
即每次调用api,获取参数的时候,都需要重新加载这个文件即可

⑷ java中conf.properties为什么能实现动态配置

因为在后台有对这个文件进行加载和解析。把conf.properties这个文件里面的数据全部解析到一个对象里。
然后在其他地方就可以通过这个对象来获取配置属性。
等程序写好后,以后要修改数据,直接在配置文件里修改,程序就不用修改,也不用重新编译了。

⑸ java热部署:tomcat运行中,动态修改配置文件(java文件)中的static属性并生效

<Context path="/tomcatTest" reloadable="true" docBase="E:\workplace\testProject\WebRoot"/>
第一个是容器里的项目path 要加/
第二个参数是你的workplace的路径,一般是到webroot

写个context.xml文件,放到项目的META-INF里.context.xml头部像上面那样写就可以

⑹ java应用(非web应用)中log4j.properties/xml动态修改配置文件,无需重启,就能立即生效,如何实现

DOMConfigurator.config(filename);
适用于xml配置文件
如果是properties配置文件则用
PropertyConfigurator.configure(filename);

⑺ 配置文件在java 中怎么创建

1.一般在scr下面新建一个属性文件*.properties,如a.properties

然后在Java程序中读取或操作这个属性文件。
代码实例
属性文件a.properties如下:
name=root
pass=liu
key=value

读取a.properties属性列表,与生成属性文件b.properties。代码如下:

1 import java.io.BufferedInputStream;
2 import java.io.FileInputStream;
3 import java.io.FileOutputStream;
4 import java.io.InputStream;
5 import java.util.Iterator;
6 import java.util.Properties;
7
8 public class PropertyTest {
9 public static void main(String[] args) {
10 Properties prop = new Properties();
11 try{
12 //读取属性文件a.properties
13 InputStream in = new BufferedInputStream (new FileInputStream("a.properties"));
14 prop.load(in); ///加载属性列表
15 Iterator<String> it=prop.stringPropertyNames().iterator();
16 while(it.hasNext()){
17 String key=it.next();
18 System.out.println(key+":"+prop.getProperty(key));
19 }
20 in.close();
21
22 ///保存属性到b.properties文件
23 FileOutputStream oFile = new FileOutputStream("b.properties", true);//true表示追加打开
24 prop.setProperty("phone", "10086");
25 prop.store(oFile, "The New properties file");
26 oFile.close();
27 }
28 catch(Exception e){
29 System.out.println(e);
30 }
31 }
32 }
getProperty/setProperty这两个方法是分别是获取和设置属性信息。
Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串类型。

*.properties文件的注释用#。
配置数据的时候是以键值对的形式,调用的时候和修改的时候也是操作键值对。

2.当然还可以用*.xml来配置,位置一般在一个包下面。
例如com.styspace包下面的config.properties文件。
xml version="1.0" encoding="gbk"?>
<Accounts>
<Account type="by0003">
<code>100001</code>
<pass>123</pass>
<name>李四</name>
<money>1000000.00</money>
</Account>
</Accounts>

现在操作config.properties文件。
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

public class peropertiesLoaderTest {

public static void main(String[] args) throws ConfigurationException{
Configuration config = new PropertiesConfiguration("com/styspace/config.properties");
String name = config.getString("name");
System.out.println("name:" + name);
}
}

⑻ Java 配置文件.

不一定,反射可以直接修改属性,为属性写get set方法是因为这是javabean写法,是一种规范,很多框架都是用这个规范来修改和获取对象属性的,所以写这两个方法只是为了能使用这些规范开发出来的框架

⑼ spring如何动态加载配置文件,就是配置文件修改了,application.xml如何能读取到

项目,需要访问多个数据库,而且需要在服务器运行不重新启动的情况下,动态的修改spring中配置的数据源datasource,在网上找了很多资料,最后找到了适合我的方法,下面总结一下。
spring的配置文件是在容器启动的时候就加载到内存中的,如果手动改了application.xml,我们必须要重新启动服务器配置文件才会生效。而在spring中提供了一个类WebApplicationContext,这个类可以让你获得一些bean,可以修改内存中的信息,我就是通过这个类来实现的。下面是我具体的代码。

package com.southdigital.hospital;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class ChangeSpringConfig extends HttpServlet
{

private String ipAddress = "127.0.0.1";

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
//先取得servleContext对象,提供给spring的WebApplicationUtils来动态修改applicationContext.xml

ipAddress = request.getParameter("ipAddress");
System.out.println(ipAddress);

ServletContext servletContext = this.getServletContext();
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
ComboPooledDataSource cpds = (ComboPooledDataSource) applicationContext.getBean("dataSource");
cpds.setJdbcUrl("jdbc:mysql://"+ipAddress+":3306/ssh");

}

}
注意:通过这种方法修改applicationContext.xml文件的时候用c3p0,而不可以用dbcp,dbcp不支持动态修改读取到内存里面的数据。
spring 3.1已经支持了。

⑽ 如何为java 程序 写配置文件

假设有如下xml配置文件config.xml:
?xml
version="1.0"
encoding="utf-8"
?
kiyho
sinkiang
100
可以用以下代码访问:
import
org.apache.commons.configuration.configurationexception;
import
org.apache.commons.configuration.xmlconfiguration;
public
class
xmlconfigdemo
{
public
static
void
main(string[]
args)
{
try
{
xmlconfiguration
config
=
new
xmlconfiguration("config.xml");
system.out.println(config.getlist("name"));
system.out.println(config.getint("info.age"));
}
catch
(configurationexception
e)
{
e.printstacktrace();
}
}
}

阅读全文

与java动态配置文件相关的资料

热点内容
美女程序员吃大餐 浏览:208
项目二级文件夹建立规则 浏览:558
dns使用加密措施吗 浏览:172
php独立运行 浏览:530
手机sh执行命令 浏览:727
云服务器的角色 浏览:733
单片机频率比例 浏览:840
我的世界服务器如何关闭正版验证 浏览:504
如何查roid服务器上的 浏览:130
安卓手机主板如何撬芯片不掉电 浏览:249
php各个框架的优缺点 浏览:101
php1100生成数组 浏览:359
以后做平面设计好还是程序员好 浏览:552
云服务器应用管理 浏览:438
饥荒云服务器搭建过程 浏览:186
可编程控制器优点 浏览:99
压缩垃圾车说明书 浏览:28
五轮书pdf 浏览:802
单片机定时流水中断系统流水灯 浏览:701
u8如何连接服务器配置 浏览:68