核心思想
当一个类创建另一个类的时候,就产生了依赖关系,我们可以用工厂来创建类,当需要用这个类的时候,可以直接从工厂获取,这样就消除了类和类之间的依赖关系,即是控制反转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;
}
*/
}