Spring function util —— SingletonSupplier SupplierUtils

Spring function util —— SingletonSupplier SupplierUtils

前言

Spring 针对 java function 提供了两个工具 API

  • SingletonSupplier
  • SupplierUtils

SingletonSupplier

public class SingletonSupplier<T> implements Supplier<T> {

	// 包装的真正的 Supplier
	@Nullable
	private final Supplier<? extends T> instanceSupplier;

	// 缺省回调
	@Nullable
	private final Supplier<? extends T> defaultSupplier;

	// 包装的实例
	@Nullable
	private volatile T singletonInstance;

	/**
	 * 公开构造
	 */
	public SingletonSupplier(@Nullable T instance, Supplier<? extends T> defaultSupplier) {
		this.instanceSupplier = null;
		this.defaultSupplier = defaultSupplier;
		this.singletonInstance = instance;
	}
	public SingletonSupplier(@Nullable Supplier<? extends T> instanceSupplier, Supplier<? extends T> defaultSupplier) {
		this.instanceSupplier = instanceSupplier;
		this.defaultSupplier = defaultSupplier;
	}

	// 私有构造
	private SingletonSupplier(Supplier<? extends T> supplier) {
		this.instanceSupplier = supplier;
		this.defaultSupplier = null;
	}
	private SingletonSupplier(T singletonInstance) {
		this.instanceSupplier = null;
		this.defaultSupplier = null;
		this.singletonInstance = singletonInstance;
	}

	// 获取最终实例
	@Override
	@Nullable
	public T get() {
		T instance = this.singletonInstance;
		if (instance == null) {
			synchronized (this) {
				instance = this.singletonInstance;

				/**
				 * 先从 instanceSupplier 获取,获取不到就从
				 * 		defaultSupplier 获取
				 */
				if (instance == null) {
					if (this.instanceSupplier != null) {
						instance = this.instanceSupplier.get();
					}
					if (instance == null && this.defaultSupplier != null) {
						instance = this.defaultSupplier.get();
					}
					this.singletonInstance = instance;
				}
			}
		}
		return instance;
	}

	// 该方法不会返回 null 的实例
	public T obtain() {
		T instance = get();
		Assert.state(instance != null, "No instance from Supplier");
		return instance;
	}

	/**
	 * 静态构造
	 */
	public static <T> SingletonSupplier<T> of(T instance) {
		return new SingletonSupplier<>(instance);
	}
	@Nullable
	public static <T> SingletonSupplier<T> ofNullable(@Nullable T instance) {
		return (instance != null ? new SingletonSupplier<>(instance) : null);
	}
	public static <T> SingletonSupplier<T> of(Supplier<T> supplier) {
		return new SingletonSupplier<>(supplier);
	}
	@Nullable
	public static <T> SingletonSupplier<T> ofNullable(@Nullable Supplier<T> supplier) {
		return (supplier != null ? new SingletonSupplier<>(supplier) : null);
	}

}
  • 一个维护单例的 Supplier,可以分别指定
    • instance:单例实例
    • instanceSupplier:单例 Supplier
    • defaultSupplier:缺省回调
  • get 方法获取最终单例实例
  • obtainNullSafeget
  • 提供了一系列静态构造方法

SupplierUtils

public abstract class SupplierUtils {

	/**
	 * NullSafe 获取 Supplier 的实例
	 */
	@Nullable
	public static <T> T resolve(@Nullable Supplier<T> supplier) {
		return (supplier != null ? supplier.get() : null);
	}

}
  • supplier.get()NullSafe 版本
  • 配合 SingletonSupplier 食用更佳

demo

public class SingletonSupplierDemo {

    @Test
    public void test() {

        SingletonSupplier<String> supplier
                = new SingletonSupplier<>(null, () -> "default");
        System.out.println(supplier.get());

        supplier = new SingletonSupplier<>(() -> "dd", () -> "default");
        System.out.println(supplier.get());

        supplier = SingletonSupplier.ofNullable("dd");
        System.out.println(supplier.get());

        supplier = SingletonSupplier.ofNullable(() -> "dd");
        System.out.println(supplier.get());

        System.out.println(SupplierUtils.resolve(supplier));
    }
}

总结

Spring 工具类

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringUtil是一个工具类,它是Spring框架提供的一个辅助类,主要用于获取Spring容器中的Bean实例。 在Spring框架中,我们将各个组件或者对象称为Bean,这些Bean都是由Spring容器进行管理和创建的。有时候,我们需要在代码中获取Spring容器中的某个Bean实例,这时就可以使用SpringUtilSpringUtil提供了一系列静态方法,可以根据Bean的名称或者类型来获取对应的实例。通过SpringUtil,我们可以在任何地方获取到Spring容器中的Bean实例,而不需要手动去管理Bean的创建和依赖注入。 使用SpringUtil的方式很简单。首先,我们需要在Spring配置文件中将该工具类配置为一个Bean,以便让Spring容器进行管理。然后,在代码中直接调用SpringUtil的静态方法就可以获取到某个Bean的实例了。 除了获取Bean实例外,SpringUtil还提供了其他一些实用的方法,比如获取Bean的名称、判断某个Bean是否存在等等。这些方法都能帮助我们更方便地操作和管理Spring容器中的Bean。 需要注意的是,尽管SpringUtil提供了便捷的访问方式,但在实际使用中,我们应该尽量避免过多地使用该工具类。因为过度依赖SpringUtil可能会导致代码的可维护性和可测试性下降,不利于代码的拓展和重构。 总之,SpringUtilSpring框架的一个辅助工具类,能够帮助我们更方便地获取和操作Spring容器中的Bean实例,但在使用过程中需要注意适度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值