property配置

之前把设备历史数据存储的时间周期存储在了数据库中,因为以下一些原因,我打算改写到property配置文件中

1、这个周期时间的配置没有单独放一个tabel中,导致现在设备类型越来越多,每次添加或者修改都要去数据库中找,数据混杂在一起,查看不便

2、代码在启动时去读库,读完存储到map中,由于map中存储的数据种类多,还单独搞了一个java类,添加了很多的属性以及属性的get、set方法,真是鸡肋

3、既要管理java类里的属性又要管理数据库的记录,麻烦

 

开始改写

首先,添加一个配置文件,发现一个限制,这个配置文件只能写到WEB-INF目录下,如果写在src目录下的话,war包里这个配置文件就消失了,意思就是在打包的时候不会把src下的配置文件打包到war中。

于是在WEB-INF目录下添加了一个配置文件。

其次,需要写一个类来加载这个配置文件,又遇到难题,这个路径怎么获取?

我尝试了一些方法去获取路径:

1、用类的路径来获取路径

System.out.println(FrequencyProperty.class.getResource(""));
System.out.println(FrequencyProperty.class.getResource("/"));
System.out.println(FrequencyProperty.class.getClassLoader().getResource(""));
System.out.println(FrequencyProperty.class.getClassLoader().getResource("/"));
file:/D:/eclipse_x64/jboss-eap-6.2/standalone/deployments/mango.war/WEB-INF/dev/com/Socket/event/
file:/D:/eclipse_x64/jboss-eap-6.2/modules/system/layers/base/sun/jdk/main/service-loader-resources/
file:/D:/eclipse_x64/jboss-eap-6.2/modules/system/layers/base/sun/jdk/main/service-loader-resources/
file:/D:/eclipse_x64/jboss-eap-6.2/modules/system/layers/base/sun/jdk/main/service-loader-resources/

 

很奇怪,只有第一个获取到了类的路径,其他的获取到的都不知道是什么鬼

2、用系统property来获取

Set<Entry<Object, Object>> entrySet = System.getProperties().entrySet();
for (Entry<Object, Object> obj : entrySet) {
    System.out.println(obj.getKey() + " ========================== " + obj.getValue());
}
java.vendor ========================== Oracle Corporation
jboss.modules.system.pkgs ========================== org.jboss.byteman
jboss.qualified.host.name ========================== pc202102041904
logging.configuration ========================== file:/D:/eclipse_x64/jboss-eap-6.2/standalone/configuration/logging.properties
org.apache.xml.security.ignoreLineBreaks ========================== true
sun.java.launcher ========================== SUN_STANDARD
javax.xml.stream.XMLInputFactory ========================== __redirected.__XMLInputFactory
os.name ========================== Windows 7
sun.management.compiler ========================== HotSpot 64-Bit Tiered Compilers
sun.nio.ch.bugLevel ==========================
java.runtime.version ========================== 1.8.0_60-b27
java.vm.specification.vendor ========================== Oracle Corporation
jboss.server.persist.config ========================== true
sun.boot.class.path ========================== D:\eclipse_x64\jdk1.8.0_60_x64\jre\lib\resources.jar;D:\eclipse_x64\jdk1.8.0_60_x64\jre\lib\rt.jar;D:\eclipse_x64\jdk1.8.0_60_x64\jre\lib\sunrsasign.jar;D:\eclipse_x64\jdk1.8.0_60_x64\jre\lib\jsse.jar;D:\eclipse_x64\jdk1.8.0_60_x64\jre\lib\jce.jar;D:\eclipse_x64\jdk1.8.0_60_x64\jre\lib\charsets.jar;D:\eclipse_x64\jdk1.8.0_60_x64\jre\lib\jfr.jar;D:\eclipse_x64\jdk1.8.0_60_x64\jre\classes
sun.desktop ========================== windows
javax.xml.parsers.SAXParserFactory ========================== __redirected.__SAXParserFactory
javax.xml.stream.XMLEventFactory ========================== __redirected.__XMLEventFactory
javax.xml.stream.XMLOutputFactory ========================== __redirected.__XMLOutputFactory
jboss.bind.address ========================== localhost
jboss.node.name ========================== pc202102041904
user.name ========================== Administrator
java.net.preferIPv4Stack ========================== true
java.util.logging.manager ========================== org.jboss.logmanager.LogManager
java.version ========================== 1.8.0_60
jboss.home.dir ========================== D:\eclipse_x64\jboss-eap-6.2
sun.boot.library.path ========================== D:\eclipse_x64\jdk1.8.0_60_x64\jre\bin
user.language ========================== zh
user.timezone ========================== Asia/Shanghai
file.encoding.pkg ========================== sun.io
java.endorsed.dirs ========================== D:\eclipse_x64\jdk1.8.0_60_x64\jre\lib\endorsed
jboss.server.config.dir ========================== D:\eclipse_x64\jboss-eap-6.2\standalone\configuration
org.jboss.logmanager.nocolor ========================== true
sun.arch.data.model ========================== 64
sun.cpu.isalist ========================== amd64
sun.jnu.encoding ========================== GBK
file.separator ========================== \
java.class.version ========================== 52.0
java.home ========================== D:\eclipse_x64\jdk1.8.0_60_x64\jre
java.specification.name ========================== Java Platform API Specification
java.vm.info ========================== mixed mode
javax.xml.parsers.DocumentBuilderFactory ========================== __redirected.__DocumentBuilderFactory
javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom ========================== __redirected.__XPathFactory
user.country ========================== CN
java.protocol.handler.pkgs ========================== org.jboss.net.protocol|org.jboss.vfs.protocol
java.vm.version ========================== 25.60-b23
jboss.i18n.generate-proxies ========================== true
org.apache.catalina.connector.URI_ENCODING ========================== UTF-8
org.jboss.boot.log.file ========================== D:/eclipse_x64/jboss-eap-6.2/standalone/log/boot.log
os.version ========================== 6.1
path.separator ========================== ;
sun.awt.enableExtraMouseButtons ========================== true
user.variant ==========================
awt.toolkit ========================== sun.awt.windows.WToolkit
java.awt.printerjob ========================== sun.awt.windows.WPrinterJob
jboss.modules.dir ========================== D:\eclipse_x64\jboss-eap-6.2\modules
jboss.server.deploy.dir ========================== D:\eclipse_x64\jboss-eap-6.2\standalone\data\content
module.path ========================== D:/eclipse_x64/jboss-eap-6.2/modules
sun.io.unicode.encoding ========================== UnicodeLittle
user.script ==========================
java.naming.factory.url.pkgs ========================== org.jboss.as.naming.interfaces:org.jboss.ejb.client.naming
jboss.server.temp.dir ========================== D:\eclipse_x64\jboss-eap-6.2\standalone\tmp
sun.rmi.dgc.client.gcInterval ========================== 3600000
user.home ========================== C:\Users\Administrator
java.library.path ========================== D:\eclipse_x64\jdk1.8.0_60_x64\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;native;D:/eclipse_x64/jdk1.8.0_60_x64/bin/../jre/bin/server;D:/eclipse_x64/jdk1.8.0_60_x64/bin/../jre/bin;D:/eclipse_x64/jdk1.8.0_60_x64/bin/../jre/lib/amd64;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\eclipse_x64\jdk1.8.0_60_x64\bin;D:\eclipse_x64\jdk1.8.0_60_x64\jre\bin;D:\Program Files\TortoiseSVN\bin;C:\Program Files\MySQL\MySQL Server 5.6\bin;D:\Program Files\Redis\;D:\IDEA\apache-maven-3.5.2\bin;D:\Program Files\nodejs\;D:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;D:\Program Files\Microsoft SQL Server\100\Tools\Binn\;D:\Program Files\Microsoft SQL Server\100\DTS\Binn\;D:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\;D:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;D:\Program Files\Microsoft VS Code\bin;C:\Users\Administrator\AppData\Roaming\npm;E:\eclipse_4.6_x64;;.
java.specification.vendor ========================== Oracle Corporation
java.vendor.url ========================== http://java.oracle.com/
org.jboss.resolver.warning ========================== true
org.xml.sax.driver ========================== __redirected.__XMLReaderFactory
program.name ========================== JBossTools: Red Hat JBoss EAP 6.1+
java.class.path ========================== D:\eclipse_x64\jboss-eap-6.2\jboss-modules.jar
java.runtime.name ========================== Java(TM) SE Runtime Environment
java.vm.vendor ========================== Oracle Corporation
jboss.bind.address.management ========================== localhost
org.jboss.security.context.ThreadLocal ========================== true
sun.java.command ========================== org.jboss.modules.Main -mp D:/eclipse_x64/jboss-eap-6.2/modules -jaxpmodule javax.xml.jaxp-provider -jaxpmodule javax.xml.jaxp-provider org.jboss.as.standalone -b localhost --server-config=standalone.xml -Djboss.server.base.dir=D:\eclipse_x64\jboss-eap-6.2\standalone
sun.rmi.dgc.server.gcInterval ========================== 3600000
catalina.home ========================== D:\eclipse_x64\jboss-eap-6.2\standalone\tmp
java.vm.specification.name ========================== Java Virtual Machine Specification
java.vm.specification.version ========================== 1.8
javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema ========================== __redirected.__SchemaFactory
jboss.server.log.dir ========================== D:\eclipse_x64\jboss-eap-6.2\standalone\log
sun.cpu.endian ========================== little
sun.os.patch.level ========================== Service Pack 1
java.awt.graphicsenv ========================== sun.awt.Win32GraphicsEnvironment
java.awt.headless ========================== true
java.ext.dirs ========================== D:\eclipse_x64\jdk1.8.0_60_x64\jre\lib\ext;C:\Windows\Sun\Java\lib\ext
java.io.tmpdir ========================== C:\Users\ADMINI~1\AppData\Local\Temp\
java.vendor.url.bug ========================== http://bugreport.sun.com/bugreport/
jboss.server.data.dir ========================== D:\eclipse_x64\jboss-eap-6.2\standalone\data
os.arch ========================== amd64
java.vm.name ========================== Java HotSpot(TM) 64-Bit Server VM
javax.management.builder.initial ========================== org.jboss.as.jmx.PluggableMBeanServerBuilder
jboss.server.base.dir ========================== D:\eclipse_x64\jboss-eap-6.2\standalone
line.separator ==========================
org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING ========================== true
user.dir ========================== D:\eclipse_x64\jboss-eap-6.2\bin
file.encoding ========================== GBK
java.specification.version ========================== 1.8
javax.xml.datatype.DatatypeFactory ========================== __redirected.__DatatypeFactory
javax.xml.transform.TransformerFactory ========================== __redirected.__TransformerFactory
jboss.host.name ========================== pc202102041904
jboss.server.name ========================== pc202102041904

 

这个jboss.server.base.dir还是有用的

相比较这个方式1和方式2,方式一的更可取,因为方式一包含了项目的名称,这样只要截取就行,但是方法2还需要固定的加上项目名称,但是名称一旦改变,还得记着来改这里,这样不太合适

还有一点比较奇怪的地方,一开始我不想截取方式一中的字符串,我想用上级目录来获取,但是结果有点让人诧异

System.out.println(FrequencyProperty.class.getResource(""));
System.out.println(FrequencyProperty.class.getResource("../"));
System.out.println(FrequencyProperty.class.getResource("../../"));
System.out.println(FrequencyProperty.class.getResource("../../../"));
file:/D:/eclipse_x64/jboss-eap-6.2/standalone/deployments/mango.war/WEB-INF/dev/com/Socket/event/
file:/D:/eclipse_x64/jboss-eap-6.2/standalone/deployments/mango.war/WEB-INF/dev/com/Socket/
jar:file:/D:/eclipse_x64/jboss-eap-6.2/modules/system/layers/base/javax/activation/api/main/activation-1.1.1-redhat-2.jar!/com/
file:/D:/eclipse_x64/jboss-eap-6.2/modules/system/layers/base/sun/jdk/main/service-loader-resources/

 

搞不懂,除了第二个是真的上级,其他两个都是啥

看下最终的代码吧

String path = FrequencyProperty.class.getResource("").getPath();
path = path.replace("dev/com/Socket/event/", "");
InputStream in = new FileInputStream(path + "frequency.properties");
props.load(in);

 

对了,如果单纯的只跑main方法,不跑web项目,结果又是大不一样

System.out.println(FrequencyProperty.class.getResource(""));
System.out.println(FrequencyProperty.class.getResource("/"));
System.out.println(FrequencyProperty.class.getClassLoader().getResource(""));
System.out.println(FrequencyProperty.class.getClassLoader().getResource("/"));
file:/D:/eclipse_x64/workspace/mango/classes/hot/com/Socket/event/
file:/D:/eclipse_x64/workspace/mango/classes/hot/
file:/D:/eclipse_x64/workspace/mango/classes/hot/
null

 

没有时间深入去研究了,暂且就这样记录下来吧,渴死了,喝口水去

 

posted @ 2019-07-30 15:27  1156740846  阅读(451)  评论(0编辑  收藏  举报