DAO层与Service业务逻辑层的解耦实现之Factory工厂模式

Phoenix Phoenix 2018.2.26 00:00 View(106) Comment (0)
    package com.jasber.jdbc.test1;  
      
    import java.io.IOException;  
    import java.io.InputStream;  
    import java.util.Properties;  
      
    public class UserDaoFactory {  
      
        private static UserDao userDao = null;// 注意此句必须放在实例化工厂类的语句之前,否者会在运行时被置为null  
        private static UserDaoFactory userDaoFactory = new UserDaoFactory();  
      
        private UserDaoFactory() {  
            Properties properties = new Properties();  
            InputStream inputStream = UserDaoFactory.class.getClassLoader()  
                    .getResourceAsStream("daoConfig.properties");  
            try {  
                properties.load(inputStream);  
                String userDaoImpl = properties.getProperty("userDaoImpl");  
                userDao = (UserDao) Class.forName(userDaoImpl).newInstance();  
            } catch (Throwable e) {  
                throw new ExceptionInInitializerError(e);// 此问题非常严重  
            }finally{  
                try {  
                    inputStream.close();  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
            }  
        }  
      
        public static UserDaoFactory getInstance() {  
            return userDaoFactory;  
        }  
      
        public UserDao getUserDao() {  
            return userDao;  
        }  
    }  

http://blog.csdn.net/miner_jasber_yon/article/details/42595275

本文链接 https://www.mangoxo.com/blog/RLo33aoN 版权所有,转载请保留地址链接,感谢!

Latest Comments :