spring Ioc原理

关于BeanFactory(模仿Spring的Ioc)

  1. 创建bean.properties。给出两个service的全类名,根据反射生成实例对象,存入spring容器BeanMap中。
accountService = com.ruixun.service.impl.AccountServiceImpl
accountDao = com.ruixun.dao.impl.AccountDaoImpl
  1. 代码
public interface AccountDao {
    void addAccount();
}
public class AccountDaoImpl implements AccountDao {
    public void addAccount() {
        System.out.println("AccountDao account ...........");
    }
}
public interface AccountService {
    void addAccount();
}
public class AccountServiceImpl implements AccountService {
    public void addAccount() {
        System.out.println("AccountService addAccount.....");
    }
}

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

public class BeanFactory {
//    bean.properties
//    accountService = com.ruixun.service.impl.AccountServiceImpl
//    accountDao = com.ruixun.dao.impl.AccountDaoImpl
    private static Properties properties;   /*用来解析bean.properties*/
    private static Map<String,Object> beans;  /*相当于spring容器*/
    static {
        try {
            properties = new Properties();
            /*拿到配置文件bean.properties字节流*/
            InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            /*加载配置文件*/
            properties.load(in);
            beans = new HashMap<String, Object>();
            Enumeration<Object> keys = properties.keys();  /*包含accountService和accountDao*/
            while (keys.hasMoreElements()){
                String key = keys.nextElement().toString();  /*bean.properties每一行*/
                String beanPath = properties.getProperty(key);  /*假如拿到了key:accountService,返回com.ruixun.service.impl.AccountServiceImpl*/
                Object value = Class.forName(beanPath).newInstance();  /*com.ruixun.service.impl.AccountServiceImpl  反射实例化*/
                beans.put(key,value);/*控制反转成功,存入容器*/
            }

        } catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError("初始化properties错误");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static Object getBean(String beanName){
        return beans.get(beanName);
    }

    public static void main(String[] args) {
        AccountService accountService = (AccountService)BeanFactory.getBean("accountService");
        accountService.addAccount();
        AccountDao accountDao = (AccountDao)BeanFactory.getBean("accountDao");
        accountDao.addAccount();
    }
}

控制反转完成
在这里插入图片描述
上面是原理,下面看spring怎么做的

  1. 创建bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--默认构造函数,没有它,spring创建不了对象-->
    <bean id="accountService" class="com.ruixun.service.impl.AccountServiceImpl"/>
    <bean id="accountDao" class="com.ruixun.dao.impl.AccountDaoImpl"/>
</beans>

接口和实现类不变

import com.ruixun.dao.AccountDao.AccountDao;
import com.ruixun.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        AccountService accountService = (AccountService) applicationContext.getBean("accountService");
        AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
        accountService.addAccount();
        accountDao.addAccount();
    }
}

结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值