zoukankan      html  css  js  c++  java
  • 从不同的配置文件中读取配置信息

    在开发java EE 过程中遇到了太多的配置文件,今天我自己总结了下读取不同配置文件的方法:

    并且在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置文件来完成,这样就能更加灵活,也更加容易在服务器上进行更改,配置文件是不会进行编译的

    1.web.xml,在写servlet时必须用到的配置文件,当然一些配置的参数放在这里也是最方便读取的

    需求就是进行数据库的连接,而将数据库的连接信息放在web.xml配置文件中,用servlet进行数据库连接的初始化;

     1 <!-- 数据库连接的一些信息 StartUpServlet的参数配置在 <init-param>间-->
    2 <servlet>
    3 <servlet-name>userLogin</servlet-name>
    4 <servlet-class>sanitation.Util.StartUpServlet</servlet-class>
    5 <init-param>
    6 <param-name>uname</param-name>
    7 <param-value>sa</param-value>
    8 </init-param>
    9 <init-param>
    10 <param-name>connUrl</param-name>
    11 <param-value>jdbc:jtds:sqlserver://localhost:9433;DatabaseName=sanitation</param-value>
    12 </init-param>
    13 <init-param>
    14 <param-name>psw</param-name>
    15 <param-value>123@sports</param-value>
    16 </init-param>
    17
    18 <load-on-startup>2</load-on-startup>
    19 </servlet>

    <load-on-startup>2</load-on-startup>能保证该servlet在web容器加载后马上进行初始化

    servlet文件:进行配置信息的获取,连接的方法 CacheUtil.fillUser(uname,psw,connUrl);就省略不写了

     1 package sanitation.Util;
    2
    3 import javax.servlet.ServletException;
    4 import javax.servlet.http.HttpServlet;
    5
    6 import sanitation.Util.CacheUtil;
    7
    8 public class StartUpServlet extends HttpServlet {
    9 /**
    10 * 一个初始化servlet,在web加载时会自动进行数据库的连接等一系列操作
    11 */
    12 private static final long serialVersionUID = 1L;
    13
    14 protected String uname = null;
    15 protected String connUrl=null;
    16 protected String psw =null;
    17
    18 public void init() throws ServletException {
    19 /**
    20 * 利用getInitParameter()获取web.xml中的参数,而this.getServletContext().getInitParameter()可以得到
    21 * 这个servlet以外的web参数,下面的this相当于ServletConfig,(getServletContext().相当于Application,
    22 * getServletConfig().某个servlet的配置),getServletContext()获得的是 <context-param> </context-param>配置的参数信息
    23 * getServletConfig()获得的是 <init-param> </init-param>配置的参数信息
    24 * */
    25 this.connUrl= this.getInitParameter("connUrl");
    26 this.uname = this.getInitParameter("uname");
    27 this.psw = this.getInitParameter("psw");
    28
    29 CacheUtil.fillUser(uname,psw,connUrl);//Web容器启动后调用
    30 }
    31
    32 }

    读取配置文件时用到了方法getInitParameters(),而在ServletConfig和ServletContext都有getInitParameter方法, 这两个方法的都能从web.xml中获取参数
    但是有区别的:ServletConfig的getInitParameter方法是用于获得该servlet的初始化信息,即位于 <init-param> </init-param>配置的参数信息,获取的方式

    this.getServletConfigs().getInitParameter();即上面的获取方式,而ServletContext的getInitParameter方法是用于获取servlet以为的信息,即整个web配置

    内的信息,即获得的是 <context-param> </context-param>配置的参数信息 ,获取方式this.getServletContexts().getInitParameter();

    2.关于.properties 配置文件的读取操作,我们一般是利用JDK提供的java.util.Properties类来进行操作

    情景:为了数据库的配置能动态的变化,灵活性促使我们不使用硬编码,而是用配置文件

    配置文件内容如下(#为注释):

    # 以下为进行数据库连接的信息

    dbPort = localhost

    databaseName = sanitation

    dbUserName = sa

    dbPassword = 123

    # 以下为数据库表信息

    dbTable = student

    # 以下为服务器信息

    ip = 172.18.4.40

    。。。 该配置文件就是一个键值对(key-value),

    以下代码是读取配置文件的一个工具类(两种类似的方式):

    1.

    InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("***.properties"); 
    Properties p = new Properties();
    try {
    p.load(inputStream);
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    System.out.println("ip:"+p.getProperty("ip")+",port:"+p.getProperty("port"));

    2.

    import java.io.FileInputStream; 
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Properties;

    public class TestProperty {
    private Properties propertie;
    private FileInputStream inputFile;

    /**
    *
    @param filePath
    * 要读取的配置文件的路径 名称
    */
    public void initProperty(String filePath) {
    propertie = new Properties();
    try {
    inputFile = new FileInputStream(filePath);
    propertie.load(inputFile);
    inputFile.close();
    } catch (FileNotFoundException ex) {
    System.out.println("读取属性文件-失败!原因:文件路径错误或者文件不存在");
    ex.printStackTrace();
    } catch (IOException ex) {
    System.out.println("装载文件-失败!");
    ex.printStackTrace();
    }

    }

    /**
    *
    *
    @param key
    * 根据键获取对应的值
    *
    @return
    */
    public String getPropertyValue(String key) {
    if (propertie.containsKey(key)) {
    String value = propertie.getProperty(key);
    return value;
    } else {
    return null;
    }
    }
    }

    除此之外,如果我们使用框架spring,那么spring也为我们封装了一个类org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来进行

    .properties 的读取,不过spring的配置文件一般是进行某个bean的一些参数配置,配置文件testBeanConfig.properties如下:

    helloWorldBean.class=chb.demo.vo.HelloWorldBean

    helloWorldBean.helloWorld=Hello!world!

    属性文件中的"helloWordBean"名称即是Bean的别名设定,.class用于指定类来源。

    读取的代码:

    BeanDefinitionRegistry reg = new DefaultListableBeanFactory(); 
    PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(reg);
    reader.loadBeanDefinitions(new ClassPathResource("testBeanConfig.properties"));
    BeanFactory factory = (BeanFactory)reg;
    HelloWorldBean helloBean = (HelloWorldBean)factory.getBean("helloWorldBean");
    System.out.println(helloBean.getHelloWorld());


    3.普通的xml文件的读取:未完待续


     

  • 相关阅读:
    一个有趣的C语言问题
    PHP 管理树莓派
    JeeSite | 保存信息修改记录封装
    SQL Server学习内容(一)
    Java面试题大汇总
    Spring框架快速入门
    单例模式
    简要分析一下java中线程的生命周期
    mybatis中的高级查询
    mybatis中的分页插件
  • 原文地址:https://www.cnblogs.com/shenliang123/p/2418034.html
Copyright © 2011-2022 走看看