All Downloads are FREE. Search and download functionalities are using the official Maven repository.

infra.beans.factory.BeanSupplier Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017 - 2024 the original author or authors.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see [https://www.gnu.org/licenses/]
 */

package infra.beans.factory;

import java.io.Serial;
import java.io.Serializable;
import java.util.function.Supplier;

import infra.lang.Assert;
import infra.lang.Nullable;
import infra.util.ClassUtils;

/**
 * Bean instance supplier
 * 

* supports singleton or prototype *

* * @author Harry Yang * @since 4.0 2021/11/29 21:13 */ public class BeanSupplier implements Supplier, Serializable { @Serial private static final long serialVersionUID = 1L; private final String beanName; @Nullable private final Class beanType; private final BeanFactory beanFactory; @SuppressWarnings({ "unchecked", "rawtypes" }) protected BeanSupplier(BeanFactory beanFactory, String beanName, @Nullable Class beanType) { Assert.notNull(beanName, "'beanName' is required"); Assert.notNull(beanFactory, "'beanFactory' is required"); this.beanFactory = beanFactory; if (beanType == null) { beanType = beanFactory.getType(beanName); if (beanType != null) { beanType = ClassUtils.getUserClass(beanType); } } this.beanType = beanType; this.beanName = beanName; } public String getBeanName() { return beanName; } public BeanFactory getBeanFactory() { return beanFactory; } /** * Return the type of the contained bean. *

If the bean type is a CGLIB-generated class, the original user-defined * class is returned. */ @Nullable public Class getBeanType() { return beanType; } public boolean isSingleton() { return false; } @Override public T get() { return beanFactory.getBean(beanName, beanType); } // static public static BeanSupplier from(BeanFactory beanFactory, String beanName) { return from(beanFactory, null, beanName); } public static BeanSupplier from(BeanFactory beanFactory, Class beanType, String beanName) { boolean singleton = beanFactory.isSingleton(beanName); return from(beanFactory, beanType, beanName, singleton); } public static BeanSupplier from(BeanFactory beanFactory, Class beanType, String beanName, boolean singleton) { if (singleton) { return new SingletonBeanSupplier<>(beanFactory, beanName, beanType); } return new BeanSupplier<>(beanFactory, beanName, beanType); } private final static class SingletonBeanSupplier extends BeanSupplier implements Supplier { @Serial private static final long serialVersionUID = 1L; private volatile transient T instance; SingletonBeanSupplier(BeanFactory beanFactory, String beanName, @Nullable Class beanType) { super(beanFactory, beanName, beanType); } @Override public T get() { T instance = this.instance; if (instance == null) { synchronized(this) { instance = this.instance; if (instance == null) { instance = super.get(); this.instance = instance; } } } return instance; } @Override public boolean isSingleton() { return true; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy