Spring 实例时使用的策略模式

策略模式作用

将某种可能有多种实现的算法抽象为接口,具体算法通过实现策略接口来实现。这样可以适应多变的场景,通过使用策略接口达到了解耦的目的。

策略接口

// Interface responsible for creating instances corresponding to a root bean definition.
public interface InstantiationStrategy {
    
}

实例化策略的某个具体实现

public class SimpleInstantiationStrategy implements InstantiationStrategy {
	
	private static final ThreadLocal<Method> currentlyInvokedFactoryMethod = new ThreadLocal<>();
    
    /**
	 * Return the factory method currently being invoked or {@code null} if none.
	 * <p>Allows factory method implementations to determine whether the current
	 * caller is the container itself as opposed to user code.
	 */
	@Nullable
	public static Method getCurrentlyInvokedFactoryMethod() {
		return currentlyInvokedFactoryMethod.get();
	}
    
    @Override
	public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,@Nullable Object factoryBean, final Method factoryMethod, Object... args) {
		try {
			Method priorInvokedFactoryMethod = 																currentlyInvokedFactoryMethod.get();
			try {
				currentlyInvokedFactoryMethod.set(factoryMethod);
				Object result = factoryMethod.invoke(factoryBean, args);
				if (result == null) {
					result = new NullBean();
				}
				return result;
			}
			finally {
				// 设计之精髓,在调用实例化时把 factoryMethod 放入 threadLocal 中,如果创建的过程调用 factoryMethod 一次就完成了,那直接把 threadlocal 清楚避免内存泄漏;如果创建的过程中又去调用另一个 factoryMethod 创建实例,那就把在这个 factoryMethod 执行完成后,把 threalocal 恢复为调用之前的样子
				if (priorInvokedFactoryMethod != null) {									
					currentlyInvokedFactoryMethod.set(priorInvokedFactoryMethod);
				}
				else {
					currentlyInvokedFactoryMethod.remove();
				}
			}
		} 
        catch (IllegalArgumentException ex) {}
		catch (IllegalAccessException ex) {}
		catch (InvocationTargetException ex) {}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值