springIOC思想和反射

bean.properties

accountService=com.itcast.service.impl.AccountServiceImpl
accountDao=com.itcast.dao.impl.AccountDaoImpl

 

beanfactory

 

package com.itcast.factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @Date 2019/9/6 18:20
 * by mocar
 *      用反射机制获取对象
 */
public class BeanFactory {
    private static Properties properties=null;
    private static Map beans;//实例放到容器中,实现单例
    static {
        try {
            properties=new Properties();
            InputStream resourceAsStream = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            properties.load(resourceAsStream);
            beans=new HashMap();//保存实例对象,单例
          /*
            方式一
            Enumeration keys = properties.keys();
            while (keys.hasMoreElements()){
                String key = keys.nextElement().toString();//obj转string
                String value = properties.getProperty(key);//获取到实现类
                Object bean = Class.forName(value).newInstance();//通过反射机制获取对象
                beans.put(key,bean);//保存实例
                //System.out.println(key + "->" + bean);
            }*/

            Set keySet = properties.keySet();
            for (Object key : keySet) {
                String value = properties.getProperty(key.toString());
                Object bean = Class.forName(value).newInstance();//通过反射机制获取对象
                beans.put(key.toString(),bean);//保存实例
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError("properties初始化失败");
        }
    }

    public static Object getBean(String beanName){
        /*单例*/
        return beans.get(beanName);

        /*多例*/
  /*      String property = properties.getProperty(beanName);
        Object bean=null;
        *//*  通过反射获取对象
        *   不使用new关键字   降低耦合,解耦
        * *//*
        try {
            bean= Class.forName(property).newInstance();//实例化,每次都会调用默认构造函数创建对象
            //System.out.println(bean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bean;*/

    }
}
 
  

 

 

service层

package com.itcast.service.impl;

import com.itcast.dao.AccountDao;
import com.itcast.dao.impl.AccountDaoImpl;
import com.itcast.factory.BeanFactory;
import com.itcast.service.AccountService;
import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * @Date 2019/9/6 17:55
 * by mocar
 */
public class AccountServiceImpl implements AccountService {
//    private AccountDao accountDao=new AccountDaoImpl();
    private AccountDao accountDao= (AccountDao) BeanFactory.getBean("accountDao");
    private int i=0;//测试单例,多例
    public void saveAccount() {
        System.out.println(accountDao);
        accountDao.saveAccount();
        System.out.println(i);
        i++;
    }
}

dao层

package com.itcast.dao.impl;

import com.itcast.dao.AccountDao;

/**
 * @Date 2019/9/6 17:57
 * by mocar
 */
public class AccountDaoImpl implements AccountDao {
    public void saveAccount() {
        System.out.println("保存账户");
    }
}

 

你可能感兴趣的:(spring)