java用工厂类模拟spring-IoC实现

核心思想

当一个类创建另一个类的时候,就产生了依赖关系,我们可以用工厂来创建类,当需要用这个类的时候,可以直接从工厂获取,这样就消除了类和类之间的依赖关系,即是控制反转IoC。

创建maven工程,配置pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>day01_eesy_02factory</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

</project>

创建表现层,业务层,持久层的模拟实现类

持久层接口和实现类
package com.itheima.Dao;

public interface IAccountDao {
    void saveAccount();
}
package com.itheima.Dao.impl;

import com.itheima.Dao.IAccountDao;

public class AccountDaoimpl implements IAccountDao {

    public void saveAccount() {
        System.out.println("save account");
    }
}
业务层接口和实现类
package com.itheima.service;

public interface IAccountservice {
    void saveAccount();
}
package com.itheima.service.impl;

import com.itheima.Dao.IAccountDao;
import com.itheima.Dao.impl.AccountDaoimpl;
import com.itheima.factory.Beanfactory;
import com.itheima.service.IAccountservice;

public class Accountimpl implements IAccountservice {
    private IAccountDao accountDao = (IAccountDao)Beanfactory.getBean("AccountDao");
    public void saveAccount(){
        accountDao = (IAccountDao)Beanfactory.getBean("AccountDao");
        System.out.println(accountDao+"123");
        accountDao.saveAccount();

    }
}
表现层实现类
package com.itheima.UI;

import com.itheima.factory.Beanfactory;
import com.itheima.service.IAccountservice;
import com.itheima.service.impl.Accountimpl;

public class Client {
    public static void main(String[] args) {
        IAccountservice as = (IAccountservice)Beanfactory.getBean("Accountservice");
        as.saveAccount();
    }
}
用配置文件加载类

创建bean.properties文件

Accountservice=com.itheima.service.impl.Accountimpl
AccountDao=com.itheima.Dao.impl.AccountDaoimpl
创建工厂来实现类容器,并且创建类加入容器
package com.itheima.factory;

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

public class Beanfactory {
    private  static Properties pro;
    private  static Map<String,Object> beans;
    static {

        try{
            System.out.println("加载类");
            pro = new Properties();
            InputStream in  = Beanfactory.class.getClassLoader().getResourceAsStream("bean.properties");
            pro.load(in);
            beans = new HashMap<String,Object>();
            Enumeration keys=pro.keys();
            while(keys.hasMoreElements()){

                String key = keys.nextElement().toString();

                String beanpath = pro.getProperty(key);

                Object value = Class.forName(beanpath).newInstance();
                System.out.println(beanpath+"  "+key+"=="+value);
                beans.put(key,value);

            }
            in.close();
            System.out.println("加载类");
        }
        catch (Exception e){
            throw new ExceptionInInitializerError("初始化配置文件错误");
        }
    }
    public static Object getBean(String beanname){
        return beans.get(beanname);
    }
    /**
    public static Object getBean(String beanname){
        Object bean = null;

        try {
            String beanpath = pro.getProperty(beanname);
            System.out.println(beanpath);
            bean = Class.forName(beanpath).newInstance();

        }catch (Exception e){
            e.printStackTrace();
        }

        return bean;
    }
    */

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值