代码结构
bean.properties
accountService = com.itheima.service.impl.AccountServiceImpl
accountDao = com.itheima.dao.impl.AccountDaoImpl
BeanFactory.java
- 这里AccountServiceImpl对象只会创建一次,在静态代码块加载的时候创建对象,并保存在beans里,是单例的。
package com.itheima.factory;
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 props;
// 定义一个Map,用于存放我们要创建的对象,称之为容器
private static Map<String,Object> beans;
// 使用静态代码块为Properties对象赋值
static {
try {
// 实例化对象
props = new Properties();
// 获取properties文件的流对象
InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
props.load(in);
// 实例化容器
beans = new HashMap<String, Object>();
// 取出配置文件中所有的Key
Enumeration keys = props.keys();
// 遍历枚举
while(keys.hasMoreElements()){
// 取出每个key
String key = keys.nextElement().toString();
// 根据key获取value
String beanPath = props.getProperty(key);
// 反射创建对象
Object value = Class.forName(beanPath).newInstance();
// 把key和value存入容器
beans.put(key,value);
}
} catch (Exception e) {
throw new ExceptionInInitializerError("初始化properties失败!");
}
}
// 根据bean的名称获取对象
public static Object getBean(String beanName){
return beans.get(beanName);
}
}
运行结果
- 每次都创建新对象,多例的
package com.itheima.factory;
import java.io.InputStream;
import java.util.Properties;
public class BeanFactory {
// 定义一个Properties对象
private static Properties props;
// 使用静态代码块为Properties对象赋值
static {
try {
// 实例化对象
props = new Properties();
// 获取properties文件的流对象
InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
props.load(in);
} catch (Exception e) {
throw new ExceptionInInitializerError("初始化properties失败!");
}
}
// 根据bean的名称获取对象
public static Object getBean(String beanName){
Object bean = null;
try{
String beanPath = props.getProperty(beanName);
bean = Class.forName(beanPath).newInstance();//每次都会调用默认构造函数创建对象
}catch (Exception e){
e.printStackTrace();
}
return bean;
}
}
运行结果
client.java
package com.itheima.ui;
import com.itheima.factory.BeanFactory;
import com.itheima.service.IAccountService;
import com.itheima.service.impl.AccountServiceImpl;
/*
模拟一个表现层,用于调用业务层
*/
public class Client {
public static void main(String[] args) {
for(int i = 0;i < 5;i++) {
IAccountService as = (IAccountService) BeanFactory.getBean("accountService");
System.out.println(as);
as.saveAccount();
}
}
}
IAccountService.java
package com.itheima.service;
/*
账户业务层的接口
*/
public interface IAccountService {
//模拟保存账户
void saveAccount();
}
AccountServiceImpl.java
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 AccountServiceImpl implements IAccountService {
private IAccountDao accountDao = (IAccountDao) BeanFactory.getBean("accountDao");
public void saveAccount() {
int i = 1;
accountDao.saveAccount();
System.out.println(i);
i++;
}
}
IAccountDao.java
package com.itheima.dao;
/*
账户的持久层接口
*/
public interface IAccountDao {
void saveAccount();
}
AccountDaoImpl.java
package com.itheima.dao.impl;
import com.itheima.dao.IAccountDao;
/*
账户的持久层实现类
*/
public class AccountDaoImpl implements IAccountDao {
public void saveAccount() {
System.out.println("保存了账户");
}
}
参考资料
https://www.bilibili.com/video/BV1Sb411s7vP