property configuration

Before the period of historical data storage device stored in the database, because some of the following reasons, I intend to rewrite property profile

1, this period of time there is no single place to configure a tabel, resulting in the type of equipment now more and more, every time you add or modify have to go find the database, the data is mixed together, see the inconvenience

2, the library code is read at startup, it is stored in the read map, since the map data stored in multiple species, but also single out a java class, adding a lot of properties and get property, SET method, really tasteless

3, it is necessary to manage java class attributes is to manage records database, trouble

 

He began to rewrite

First, add a profile, find a limit, this configuration file can be written to the WEB-INF directory, if written in the src directory, then, war bag configuration file disappears, meaning that the package is not the time the package configuration files in src to war.

Then add a configuration file in the WEB-INF directory.

Second, the need to write a class to load the configuration file, encountered problems, how to obtain this path?

I tried a number of ways to get the path:

1, with the path to obtain a path type

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/

 

Very strange, only the first path to get to the class, others do not get to know what the hell

2, the system used to obtain the 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

 

This is useful jboss.server.base.dir

This compared to embodiments 1 and 2, a more preferable embodiment, since the embodiment contains the name of a project, so long as the intercept on the line, but the method requires a fixed plus 2 further item name, but the name changed once, have to remember to change here, this is not appropriate

Another point is rather strange place, I do not want to start a way to intercept the string, I want to get the parent directory, but the results somewhat astonishing

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/

 

Do not understand, except that the second one is really superior, the other two are Han

The final look of the bar code

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

 

By the way, if only a simple main method to run, do not run the web project, the result is quite different

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

 

We do not have time to study in depth, and for the time being so recorded it, thirst, to drink water

 

Guess you like

Origin www.cnblogs.com/LcxSummer/p/11270324.html
Recommended