Spring 使用配置文件+反射機制+工廠模式模擬IOC解耦(單例)案例

使用配置文件+反射機制+工廠模式模擬IOC解耦(單例)案例

(1)客戶端:

package com.njau.UI;

import com.njau.Util.BeanFactory;
import com.njau.service.AccountService;

public class Client {


    public static void main(String[] args) {

        //AccountService accountService = new AccountServiceImpl();

        AccountService accountService = (AccountService)BeanFactory.getBean("accountService");
        accountService.saveData();

    }
}

(2)service接口

package com.njau.service;

public interface AccountService {
    //模擬保存數據
    void saveData();
}

service接口實現類

package com.njau.service.serviceImpl;

import com.njau.Util.BeanFactory;
import com.njau.dao.AccountDao;
import com.njau.dao.daoimpl.AccountDaoImpl;
import com.njau.service.AccountService;

public class AccountServiceImpl implements AccountService {

    //private AccountDao accountDao = new AccountDaoImpl();

    private AccountDao accountDao = (AccountDao)BeanFactory.getBean("accountDao");
    @Override
    public void saveData() {
        accountDao.saveData();
    }
}

(3)dao接口

package com.njau.dao;

public interface AccountDao {
    //模擬保存數據
    void saveData();
}

dao接口實現類

package com.njau.dao.daoimpl;

import com.njau.dao.AccountDao;

public class AccountDaoImpl implements AccountDao {
    @Override
    public void saveData() {
        System.out.println("保存成功!!");
    }
}

(4)配置文件解析(含反射機制與工廠模式)

package com.njau.Util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class BeanFactory {
    //定義一個Properties對象
    private static Properties properties;

    //定義一個Map,用於存放我們要創建的對象,我們把它稱之爲容器
    private static Map<String,Object> beans;

    //使用靜態代碼塊爲Properties對象賦值
    static {
        //實例化對象
        properties = new Properties();
        //獲取properties對象的流對象
        InputStream inputStream = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
        try {
            properties.load(inputStream);
            //實例化容器
            beans = new HashMap<String, Object>();
            //取出配置文件中所有的key
            Enumeration<Object> keys = properties.keys();
            //遍歷枚舉
            while (keys.hasMoreElements()){
                //從枚舉中取出所有的key
                String key = keys.nextElement().toString();
                System.out.println(key);
                //通過key,取出配置文件中對應的值,即全限定類名
                String beanPath = properties.getProperty(key);
                //通過全限定類名用反射的方式創建實例
                Object instance = Class.forName(beanPath).newInstance();
                //將key和實例存入map集合中,即存入beans中
                beans.put(key,instance);
            }
        } catch (IOException e) {
            e.printStackTrace();
            //初始化異常
            throw new ExceptionInInitializerError("讀取bean配置文件出錯");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("在遍歷keys時,可能出現了異常");
        }
    }

    /**
     * 通過beanName獲取對象(單例)
     * @param beanName
     * @return
     */
    public static Object getBean(String beanName){
        Object bean = beans.get(beanName);
        return bean;
    }

}

(5)配置文件

accountService=com.njau.service.serviceImpl.AccountServiceImpl
accountDao=com.njau.dao.daoimpl.AccountDaoImpl

參考:

https://blog.csdn.net/weixin_44268792/article/details/99207563

表示感謝!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章