欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

读取xml文件中的配置参数实例

程序员文章站 2024-02-26 08:30:34
paras.xml文件

paras.xml文件

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemalocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
   
   
   <bean id="sysparam" class="com.wisoft.tysfrz.utils.sysparam">
    <!--行政区划代码前台显示默认值-->
    <property name="zonecode" value="36"></property>
    <!--二代证读卡器读取照片存储位置-->
    <property name="sfzpicpath" value="d:\\"></property>
    <!--保存身份证照片相对路径-->
    <property name="photorealpath" value="r/project/imgs/photo/"></property>
  </bean>
</beans>

sysparam.java类文件

package com.wisoft.tysfrz.utils;
/**
 * 系统配置参数
 * 
 * @author zhenwencan
 * @date 2017年10月9日 下午1:09:48
 */
public class sysparam {
 //行政区划代码前台显示默认值
 private string zonecode;
 //二代证读卡器读取照片存储位置
 private string sfzpicpath;
 //保存身份证照片相对路径
 private string photorealpath;
 public string getzonecode() {
  return zonecode;
 }
 public void setzonecode(string zonecode) {
  this.zonecode = zonecode;
 }
 public string getsfzpicpath() {
  return sfzpicpath;
 }
 public void setsfzpicpath(string sfzpicpath) {
  this.sfzpicpath = sfzpicpath;
 }
 public string getphotorealpath() {
  return photorealpath;
 }
 public void setphotorealpath(string photorealpath) {
  this.photorealpath = photorealpath;
 }
}

使用

private static applicationcontext cpxac = new classpathxmlapplicationcontext("tysfrz/spring/tysfrz_params.xml");
private static sysparam sysparam = (sysparam) cpxac.getbean("sysparam");

string photorealpath = sysparam.getphotorealpath();

需要引用包

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

以上这篇读取xml文件中的配置参数实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。