Spring bean 源码 --- BeanFactory

1、 Bean 的生命周期

 * <ol>
 * <li>BeanNameAware's {@code setBeanName}
 * <li>BeanClassLoaderAware's {@code setBeanClassLoader}
 * <li>BeanFactoryAware's {@code setBeanFactory}
 * <li>EnvironmentAware's {@code setEnvironment}
 * <li>EmbeddedValueResolverAware's {@code setEmbeddedValueResolver}
 * <li>ResourceLoaderAware's {@code setResourceLoader}
 * (only applicable when running in an application context)
 * <li>ApplicationEventPublisherAware's {@code setApplicationEventPublisher}
 * (only applicable when running in an application context)
 * <li>MessageSourceAware's {@code setMessageSource}
 * (only applicable when running in an application context)
 * <li>ApplicationContextAware's {@code setApplicationContext}
 * (only applicable when running in an application context)
 * <li>ServletContextAware's {@code setServletContext}
 * (only applicable when running in a web application context)
 * <li>{@code postProcessBeforeInitialization} methods of BeanPostProcessors
 * <li>InitializingBean's {@code afterPropertiesSet}
 * <li>a custom init-method definition
 * <li>{@code postProcessAfterInitialization} methods of BeanPostProcessors
 * </ol>
 *
 * <p>On shutdown of a bean factory, the following lifecycle methods apply:
 * <ol>
 * <li>{@code postProcessBeforeDestruction} methods of DestructionAwareBeanPostProcessors
 * <li>DisposableBean's {@code destroy}
 * <li>a custom destroy-method definition
 * </ol>

 这个是Bean 运行的生命周期。

   ?  这个不太明白,后续补上。

/**
	 * Used to dereference a {@link FactoryBean} instance and distinguish it from
	 * beans <i>created</i> by the FactoryBean. For example, if the bean named
	 * {@code myJndiObject} is a FactoryBean, getting {@code &myJndiObject}
	 * will return the factory, not the instance returned by the factory.
	 */
String FACTORY_BEAN_PREFIX = "&";

  ?通过name 进行实例化对象。

Object getBean(String name) throws BeansException;

? 这个方法添加指定实例化bean的类型。

<T> T getBean(String name, Class<T> requiredType) throws BeansException;

?这个通过指定实例化的构造函数参数。

Object getBean(String name, Object... args) throws BeansException;

?通过类型进行匹配进行实例化,这个无参构造函数进行实例化。

<T> T getBean(Class<T> requiredType) throws BeansException;

?通过匹配的类型,构造函数进行实例化。

<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;

?构造一个类型提供器。

<T> ObjectProvider<T> getBeanProvider(Class<T> requiredType);

  这个看下其实现,发现内部也是进行实例化,进行了一层的封装,暂不清楚为啥会在进行封装一个这个类。

@Override
	public <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType) {
		return new ObjectProvider<T>() {
			@Override
			public T getObject() throws BeansException {
				return getBean(requiredType);
			}
			@Override
			public T getObject(Object... args) throws BeansException {
				return getBean(requiredType, args);
			}
			@Override
			@Nullable
			public T getIfAvailable() throws BeansException {
				try {
					return getBean(requiredType);
				}
				catch (NoUniqueBeanDefinitionException ex) {
					throw ex;
				}
				catch (NoSuchBeanDefinitionException ex) {
					return null;
				}
			}
			@Override
			@Nullable
			public T getIfUnique() throws BeansException {
				try {
					return getBean(requiredType);
				}
				catch (NoSuchBeanDefinitionException ex) {
					return null;
				}
			}
		};
	}

  看ObjectProvider 类 

/**
 * A variant of {@link ObjectFactory} designed specifically for injection points,
 * allowing for programmatic optionality and lenient not-unique handling.
 */

  在这个类中提供了这个方法

@Override
	default Iterator<T> iterator() {
		return stream().iterator();
	}

说明通过这类可以遍历处理多个实例。而BeanFactory 的其他方法都是返回一个实例对象。这里加载多个相同类的实例provider。

从注释可以发现这个是5.1 增加的方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值