日期:2014-05-20  浏览次数:20895 次

问两个个简单的问题,和静态类,classLoader有关。

package cn.dzr.jdbc.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;


public class DaoFactory {
   private static UserDao dao = null;
   private static DaoFactory instance = new DaoFactory();
   
   
   private DaoFactory()
   {
       
       try
       {
           System.out.println("进入try方法");
            Properties prop = new Properties();
            File file = new File("src/daoconfig.properties");
            InputStream in = new FileInputStream(file);
            in = DaoFactory.class.getClassLoader().getResourceAsStream("daoconfig.properties");
            prop.load(in);
            String userDaoName = prop.getProperty("UserDaoClass");
            System.out.println(userDaoName);
            Class clazz = Class.forName(userDaoName);
            System.out.println("类加载完成!!");
            dao = (UserDao)clazz.newInstance();
            System.out.println("创建类成功!"+dao.getClass().getName());
       }
       catch(Exception e)
       {
           throw new ExceptionInInitializerError(e);
           //初始化错误!
       }
   }
   
   public static DaoFactory getInstance()
   {
       return instance;
   }
   
   public UserDao getUserDao()