java配置文件事什么意思,什么是JavaConfig
其实java配置文件事什么意思的问题并不复杂,但是又很多的朋友都不太了解什么是JavaConfig,因此呢,今天小编就来为大家分享java配置文件事什么意思的一些知识,希望可以帮助到大家,下面我们一起来看看这个问题的分析吧!
配置文件在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 怎么读取配置文件
一.读取xml配置文件
(一)新建一个java bean(HelloBean. java)
java代码
(二)构造一个配置文件(beanConfig.xml)
xml代码
(三)读取xml文件
1.利用ClassPathXmlApplicationContext
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.PropertiesBeanDefinitionReader来读取属性文件
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();
什么是JavaConfig
config它是英文单词configuration的缩写,是“配置”的意思一般是修改系统配置或设置的,计算机中各类软件及系统都有类似CONFIG的文件,其中主要是系统或各软件的配置参数。
如config.sys中记录了电脑系统启动时加载的配置参数。
一般软件config.ini中记录了软件运行时需要带的参数或运行环境。
还有些软件将参数文件以*.cfg为文件名,而CFG也是config的缩写。
求解java使用配置文件的好处是什么
额你一看就没工作过我给你举个例子
当一个项目方到生产环境,开发人员不是会维护的有专门的维护人员,如果有些地方需要修改,维护人员更改配置文件重启ok了
如果在代码中呢?需要开发人员修改源码然后编译然后替换?你觉得哪个工作量大?而且更改配置文件改一个地方?但这个配置属性在代码中多处用到如果去代码中岂不是要改很多地方?还有很多优点自己慢慢感受吧
OK,关于java配置文件事什么意思和什么是JavaConfig的内容到此结束了,希望对大家有所帮助。