简体   繁体   English

如何在XML中配置属性?

[英]How to configure properties in XML?

I am planing to Maven develop a module that actually needs to read configuration data something like below, where tag <path> will be repeated multiple times with different path name and can be same <pathtype> and different URL and parameters and then attempt to access those URLs and paths in Java classes. 我计划Maven开发一个实际上需要读取配置数据的模块,如下所示,其中标记<path>将使用不同的路径名重复多次,并且可以使用相同的<pathtype>和不同的URL和参数,然后尝试访问Java类中的那些URL和路径。

In Java I should be able to read all <pathurl> tags and its parameters for given type. 在Java中,我应该能够读取给定类型的所有<pathurl>标记及其参数。 then I will process them in Java one by one. 然后我将用Java一步一步地处理它们。

What is best way to configure this data and read in Java? 什么是配置此数据并用Java读取的最佳方法? Should it be normal properties file loading process by Java or there is any new best practice using spring or any other utility. 它是Java的正常属性文件加载过程,还是使用spring或任何其他实用程序的新最佳实践。

Basically I want to know is there any way of configuring data other than in this XML format and then parsing it? 基本上,我想知道除了以这种XML格式配置数据外,还没有其他方法可以对其进行解析吗? Since the data is static in that XML, my client don't want to use database. 由于该XML中的数据是静态的,因此我的客户端不想使用数据库。

<path>
  <pathname>mypath</pathname>
  <pathtype>httpfile</pathtype>
  <pathurl>http://acdsds:8380/gis/</pathurl>
  <params>
    <user>sad</user>
    <password>spwd</password>
    <httprescd>100</httprescd>
    <mexist>DROD_MEF.gif</mexist>
  </params>
</path>

When it comes to configuring software using certain properties such as URL's, passwords and unique keys in spring PropertyPlaceHolderConfigurer is your best bet. 在春季使用某些属性(例如URL,密码和唯一键)配置软件时, PropertyPlaceHolderConfigurer是您的最佳选择。

From spring document: 从春季文件:

You use the PropertyPlaceholderConfigurer to externalize property values from a bean definition in a separate file using the standard Java Properties format. Doing so enables the person deploying an application to customize environment-specific properties such as database URLs and passwords, without the complexity or risk of modifying the main XML definition file or files for the container.

What you will do is, put all your configuration data in a properties file: 您要做的是,将所有配置数据放入属性文件中:

##JDBC related properties start here##
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseURL=jdbc:mysql://localhost:3306/databaseName
jdbc.userName=root
jdbc.password=root  
##JDBC related properties end here##
## path Configuration start here##
path.name=mypath
path.type=httpfile
path.url=http://acdsds:8380/gis/
## path Configuration ends here##

Then, configure spring to access external properties file(assuming your properties file is named settings.properties): 然后,配置spring来访问外部属性文件(假设您的属性文件名为settings.properties):

<!--settings for accessing external property files--> 
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>                  
                <value>/yourPathToPropertiesFile/settings.properties</value>                   
            </list>
        </property>
    </bean>  

After you have configured PropertyPlaceholderConfigurer, access your properties by simple using @value annotation, wherever you want. 配置PropertyPlaceholderConfigurer后,可以在任意位置使用@value注释简单地访问属性。

@Value("${path.name}")
String pathName;

You can even use properties file to configure you data source and many other stuff: 您甚至可以使用属性文件来配置数据源和许多其他内容:

<bean id="dataSource" destroy-method="close"
        class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.databaseURL}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

You should probably start by reading some basic tutorials on XML processing in Java. 您可能应该先阅读一些有关Java中XML处理的基本教程。 I'm biased, but my standard recommendation is still the material at IBM's DeveloperWorks XML site 我有偏见,但我的标准建议仍然是IBM DeveloperWorks XML站点上的材料。

You can use json configuration file and Google's Gson( https://code.google.com/p/google-gson/ ) to parse the json file. 您可以使用json配置文件和Google的Gson( https://code.google.com/p/google-gson/ )来解析json文件。 Refer to this thread Parse a nested JSON using gson on how to use Gson library & sample json format. 请参阅此线程, 使用gson解析嵌套的JSON ,了解如何使用Gson库和示例json格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM