java模拟spring三级缓存解决循环依赖
运行环境 要jdk8
package com.wwz.spring.circle_dependence;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class Main {
public static void main(String[] args) {
// 初始化BeanFactory
BeanFactory beanFactory = new BeanFactory();
// 注册A的工厂
beanFactory.registerSingletonFactory("A", new AFactory(beanFactory));
// 注册B的工厂
beanFactory.registerSingletonFactory("B", new BFactory(beanFactory));
// 获取A Bean并触发依赖注入
A a = (A) beanFactory.getBean("A");
a.sayHello(); // 输出: A says hello, B's name is B
System.out.println(a);
// 获取B Bean并触发依赖注入
B b = (B) beanFactory.getBean("B");
b.sayHello(); // 输出: B says hello, A's name is A
System.out.println(b);
}
}
// A Bean类
class A {
private B b;
public void setB(B b) {
this.b = b;
}
public void sayHello() {
System.out.println("A says hello, B's name is " + b.getName());
}
public String getName() {
return "A";
}
}
// B Bean类
class B {
private A a;
public void setA(A a) {
this.a = a;
}
public void sayHello() {
System.out.println("B says hello, A's name is " + a.getName());
}
public String getName() {
return "B";
}
}
// BeanFactory模拟Spring的IOC容器
class BeanFactory {
private final Map singletonObjects = new HashMap<>(); // 一级缓存
private final Map earlySingletonObjects = new HashMap<>(); // 二级缓存
private final Map singletonFactories = new HashMap<>(); // 三级缓存
// 注册Bean工厂
public void registerSingletonFactory(String beanName, FactoryBean factory) {
singletonFactories.put(beanName, factory);
}
// 获取Bean
public Object getBean(String beanName) {
// 如果已经实例化,直接返回
if (singletonObjects.containsKey(beanName)) {
return singletonObjects.get(beanName);
}
// 如果在二级缓存中,则直接返回
if (earlySingletonObjects.containsKey(beanName)) {
return earlySingletonObjects.get(beanName);
}
// 获取工厂方法并创建Bean
FactoryBean factory = singletonFactories.get(beanName);
if (factory == null) {
throw new IllegalArgumentException("No bean registered with name: " + beanName);
}
// 创建bean并将其放入二级缓存中
earlySingletonObjects.put(beanName, null); // 表示正在创建Bean
Object bean = factory.create();
System.out.println(bean);
earlySingletonObjects.put(beanName, bean); // 完成创建后放入earlySingletonObjects
// 完成初始化后,放入一级缓存并清除二级缓存
singletonObjects.put(beanName, bean);
earlySingletonObjects.remove(beanName);
return bean;
}
}
// 工厂接口,用于创建Bean
interface FactoryBean {
Object create();
}
// A的工厂类
class AFactory implements FactoryBean {
private final BeanFactory beanFactory;
public AFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
@Override
public Object create() {
// 创建A对象
A a = new A();
System.out.println("Creating A inside factory...");
// 将B注入到A中,B是一个代理对象
a.setB((B) createProxy(B.class, beanFactory));
return a;
}
private Object createProxy(Class> clazz, BeanFactory beanFactory) {
// 使用CGLIB创建代理
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(clazz); // 设置父类为B
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
if (method.getName().equals("getName")) {
// 当调用getName方法时,返回B的名字
return "B";
}
return method.invoke(beanFactory.getBean("B"), args); // 调用B的实际方法
}
});
return enhancer.create(); // 创建B的代理对象
}
}
// B的工厂类
class BFactory implements FactoryBean {
private final BeanFactory beanFactory;
public BFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
@Override
public Object create() {
// 创建B对象
B b = new B();
System.out.println("Creating B inside factory...");
// 将A注入到B中,A是一个代理对象
b.setA((A) createProxy(A.class, beanFactory));
return b;
}
private Object createProxy(Class> clazz, BeanFactory beanFactory) {
// 使用CGLIB创建代理
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(clazz); // 设置父类为A
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
if (method.getName().equals("getName")) {
// 当调用getName方法时,返回A的名字
return "A";
}
return method.invoke(beanFactory.getBean("A"), args); // 调用A的实际方法
}
});
return enhancer.create(); // 创建A的代理对象
}
}
正文到此结束
- 本文标签: Java Spring
- 本文链接: https://www.wangwenzhi.top/article/33
- 版权声明: 本文由王开心原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权