1、创建你的pojo对象
| package cn.pojo; |
| |
| |
| |
| |
| |
| public class UserDao { |
| public void queryUserInfo(){ |
| System.out.println("hello world!!!"); |
| } |
| } |
| |
2、创建resources文件夹并创建beans.properties文件
注意:cn.pojo.UserDao要对应填写你自己创建的

3、定义Bean
| package cn.bean; |
| |
| |
| |
| |
| |
| |
| |
| public class BeanDefinition { |
| private String beanName; |
| |
| private Class beanClass; |
| |
| public String getBeanName() { |
| |
| return beanName; |
| } |
| |
| public void setBeanName(String beanName) { |
| |
| this.beanName = beanName; |
| } |
| |
| public Class getBeanClass() { |
| |
| return beanClass; |
| } |
| |
| public void setBeanClass(Class beanClass) { |
| |
| this.beanClass = beanClass; |
| } |
| } |
| |
4、资源加载
| package cn.utils; |
| |
| import cn.bean.BeanDefinition; |
| |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.util.HashMap; |
| import java.util.Iterator; |
| import java.util.Map; |
| import java.util.Properties; |
| |
| |
| |
| |
| |
| public class ResourceLoader { |
| public static Map<String, BeanDefinition> getResource() { |
| Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>(16); |
| Properties properties = new Properties(); |
| try { |
| InputStream inputStream = ResourceLoader.class.getResourceAsStream("/beans.properties"); |
| properties.load(inputStream); |
| Iterator<String> it = properties.stringPropertyNames().iterator(); |
| while (it.hasNext()) { |
| String key = it.next(); |
| String className = properties.getProperty(key); |
| BeanDefinition beanDefinition = new BeanDefinition(); |
| beanDefinition.setBeanName(key); |
| Class clazz = Class.forName(className); |
| beanDefinition.setBeanClass(clazz); |
| beanDefinitionMap.put(key, beanDefinition); |
| } |
| inputStream.close(); |
| } catch (IOException | ClassNotFoundException e) { |
| e.printStackTrace(); |
| } |
| return beanDefinitionMap; |
| } |
| } |
| |
5、Bean注册
| package cn.bean; |
| |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| |
| |
| |
| |
| |
| |
| public class BeanRegister { |
| |
| |
| private Map<String, Object> singletonMap = new HashMap(32); |
| |
| |
| |
| |
| |
| |
| |
| public Object getSingletonBean(String beanName) { |
| return singletonMap.get(beanName); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public void registerSingletonBean(String beanName, Object bean) { |
| if (singletonMap.containsKey(beanName)) { |
| return; |
| } |
| singletonMap.put(beanName, bean); |
| } |
| } |
| |
6、对象工厂
| package cn.factory; |
| |
| import cn.bean.BeanDefinition; |
| import cn.bean.BeanRegister; |
| import cn.utils.ResourceLoader; |
| |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| |
| |
| |
| |
| |
| |
| public class BeanFactory { |
| private Map<String, BeanDefinition> beanDefinitionMap = new HashMap(); |
| |
| private BeanRegister beanRegister; |
| |
| public BeanFactory() { |
| |
| beanRegister = new BeanRegister(); |
| |
| this.beanDefinitionMap = ResourceLoader.getResource(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public Object getBean(String beanName) { |
| |
| Object bean = beanRegister.getSingletonBean(beanName); |
| if (bean != null) { |
| return bean; |
| } |
| |
| return createBean(beanDefinitionMap.get(beanName)); |
| } |
| |
| |
| |
| |
| |
| |
| |
| private Object createBean(BeanDefinition beanDefinition) { |
| try { |
| Object bean = beanDefinition.getBeanClass().newInstance(); |
| |
| beanRegister.registerSingletonBean(beanDefinition.getBeanName(), bean); |
| return bean; |
| } catch (InstantiationException | IllegalAccessException e) { |
| e.printStackTrace(); |
| } |
| return null; |
| } |
| } |
| |
7、测试
| package cn; |
| |
| import cn.factory.BeanFactory; |
| import cn.pojo.UserDao; |
| |
| |
| |
| |
| |
| public class ApiTest { |
| public static void main(String[] args) { |
| |
| BeanFactory beanFactory = new BeanFactory(); |
| |
| |
| UserDao userDao1 = (UserDao) beanFactory.getBean("userDao"); |
| userDao1.queryUserInfo(); |
| |
| |
| UserDao userDao2 = (UserDao) beanFactory.getBean("userDao"); |
| userDao2.queryUserInfo(); |
| } |
| } |
| |
效果图:

总结:实际上先通过spi的方式把编译前的bean资源加载,在工厂方法中初始化,在调用bean的时候通过单例的方式获取,如果是第一次获取就通过加载过的资源实例化bean,如果不是就根据bean名称返回bean
关注我的公众号SpaceObj 领取idea系列激活码

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)