注:以下内容为学习《小马哥讲Spring核心编程思想》的个人笔记
单一类型依赖查找
单一类型依赖查找接口BeanFactoy
package org.springframework.beans.factory;
import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;
public interface BeanFactory {
// 通过Bean的名称查找
Object getBean(String name) throws BeansException;
// 通过Bean的名称和类型进行查找
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
// 通过类型查找
<T> T getBean(Class<T> requiredType) throws BeansException;
// 通过类型延迟查找
<T> ObjectProvider<T> getBeanProvider(Class<T> requiredType);
// 和java.lang.reflect.Type类有关, 可以通过supertypes, interfaces和generic parameters进行依赖查找(嗯,我也不是很清楚)
<T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType);
}
集合类型依赖查找
集合类型的依赖查找接口ListableBeanFactory, 该接口继承BeanFactory,是对BeanFactory的增强
package org.springframework.beans.factory;
import java.lang.annotation.Annotation;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;
public interface ListableBeanFactory extends BeanFactory {
// 通过类型查询Bean的名称
String[] getBeanNamesForType(ResolvableType type);
String[] getBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit);
String[] getBeanNamesForType(@Nullable Class<?> type);
String[] getBeanNamesForType(@Nullable Class<?> type, boolean includeNonSingletons, boolean allowEagerInit);
// 通过注解查询Bean的名称
String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType);
// 通过类型查询Bean的实例
<T> Map<String, T> getBeansOfType(@Nullable Class<T> type) throws BeansException;
<T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
throws BeansException;
// 通过注解查询Bean的实例
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;
}
层次性依赖查找
层次性依赖查找接口HierarchicalBeanFactory
package org.springframework.beans.factory;
import org.springframework.lang.Nullable;
public interface HierarchicalBeanFactory extends BeanFactory {
// 得到父BeanFactory
@Nullable
BeanFactory getParentBeanFactory();
// 判断当前BeanFacory是否包含Bean
boolean containsLocalBean(String name);
}
集合,层次,单一类型依赖查找的组合(ConfigurableListableBeanFactory)
ConfigurableListableBeanFactory接口继承了:
- ListableBeanFactory接口(集合类型查找)
- ConfigurableBeanFactory接口,ConfigurableBeanFactory接口又继承了HierarchicalBeanFactory接口(层次性查找)
ConfigurableListableBeanFactory接口将单一类型依赖查找,集合类型依赖查找,层次性依赖查找组合成在一起,提供了更强大的查找功能。
package org.springframework.beans.factory.config;
import java.util.Iterator;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.lang.Nullable;
public interface ConfigurableListableBeanFactory
extends ListableBeanFactory, AutowireCapableBeanFactory, ConfigurableBeanFactory {
}
延迟依赖查找
延迟依赖查找方法是org.springframework.beans.factory.BeanFactory#getBeanProvider(java.lang.Class),getBeanProvider返回了一个实现ObjectProvider接口的对象。ObjectProvider接口继承了ObjectFactory。
ObjectFactory和BeanFactory区别:https://blog.csdn.net/meism5/article/details/107601864
ObjectFactory 与 BeanFactory 均提供依赖查找的能力。
ObjectFactory 仅关注一个或一种类型的 Bean 依赖查找,自身不具备依赖查找的能力,能力由 BeanFactory 输出;BeanFactory 提供了单一类型、集合类型以及层次性等多种依赖查找的方式。
package org.springframework.beans.factory;
import org.springframework.beans.BeansException;
@FunctionalInterface
public interface ObjectFactory<T> {
T getObject() throws BeansException;
}
package org.springframework.beans.factory;
import java.util.Iterator;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;
public interface ObjectProvider<T> extends ObjectFactory<T>, Iterable<T> {
@Nullable
T getIfAvailable() throws BeansException;
default T getIfAvailable(Supplier<T> defaultSupplier) throws BeansException {
T dependency = getIfAvailable();
return (dependency != null ? dependency : defaultSupplier.get());
}
}
安全依赖查找
安全不安全依赖查找值得是否有NoSuchBeanDefinitionException或者NoUniqueBeanDefinitionException异常,看接口定义,单一类型依赖查找是非安全的。其他(集合类型和延迟依赖查找)是安全的。
/**
* @throws NoSuchBeanDefinitionException if no bean of the given type was found
* @throws NoUniqueBeanDefinitionException if more than one bean of the given type was found
* @throws BeansException if the bean could not be created
* @since 3.0
* @see ListableBeanFactory
*/
// 单一类型查找接口
<T> T getBean(Class<T> requiredType) throws BeansException;
/**
* @throws BeansException if a bean could not be created
* @see FactoryBean#getObjectType
* @see BeanFactoryUtils#beansOfTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)
*/
// 集合类型依赖查找接口
<T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
throws BeansException;
内建可查找的依赖
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {
/**
* Name of the MessageSource bean in the factory.
* If none is supplied, message resolution is delegated to the parent.
* @see MessageSource
*/
public static final String MESSAGE_SOURCE_BEAN_NAME = "messageSource";
/**
* Name of the LifecycleProcessor bean in the factory.
* If none is supplied, a DefaultLifecycleProcessor is used.
* @see org.springframework.context.LifecycleProcessor
* @see org.springframework.context.support.DefaultLifecycleProcessor
*/
public static final String LIFECYCLE_PROCESSOR_BEAN_NAME = "lifecycleProcessor";
/**
* Name of the ApplicationEventMulticaster bean in the factory.
* If none is supplied, a default SimpleApplicationEventMulticaster is used.
* @see org.springframework.context.event.ApplicationEventMulticaster
* @see org.springframework.context.event.SimpleApplicationEventMulticaster
*/
public static final String APPLICATION_EVENT_MULTICASTER_BEAN_NAME = "applicationEventMulticaster";
}
public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable {
/**
* Name of the ConversionService bean in the factory.
* If none is supplied, default conversion rules apply.
* @since 3.0
* @see org.springframework.core.convert.ConversionService
*/
String CONVERSION_SERVICE_BEAN_NAME = "conversionService";
/**
* Name of the LoadTimeWeaver bean in the factory. If such a bean is supplied,
* the context will use a temporary ClassLoader for type matching, in order
* to allow the LoadTimeWeaver to process all actual bean classes.
* @since 2.5
* @see org.springframework.instrument.classloading.LoadTimeWeaver
*/
String LOAD_TIME_WEAVER_BEAN_NAME = "loadTimeWeaver";
/**
* Name of the {@link Environment} bean in the factory.
* @since 3.1
*/
String ENVIRONMENT_BEAN_NAME = "environment";
/**
* Name of the System properties bean in the factory.
* @see java.lang.System#getProperties()
*/
String SYSTEM_PROPERTIES_BEAN_NAME = "systemProperties";
/**
* Name of the System environment bean in the factory.
* @see java.lang.System#getenv()
*/
String SYSTEM_ENVIRONMENT_BEAN_NAME = "systemEnvironment";
}
依赖查找中的经典异常
异常类型 | 说明 |
---|---|
NoSuchBeanDefinitionException | 查找的Bean在容器中不存在 |
NoUniqueBeanDefinitionExceptio | 单一类型查找时出现多个Bean实例 |
BeanInstantiationException | 查找的类不是接口或者抽象类,实例化时抛出异常 |
BeanCreationException | 初始化(设置Bean属性)时抛出异常 |
BeanDefinitionStoreException | 当 BeanDefinition 配置元信息非法 |
有用的广告
我是程序员,也热爱投资。个人认为,程序员这种职业(大部分是青春饭)非常适合投资,在年轻的积累本金,35岁后能过有睡后收入,想想就美滋滋。当然,如果没有专业的投资知识就是在股市中韭菜的份,这里推广下我学习投资知识的地方。简单说明下,这两个群都不会荐股,只是告诉你投资理念和知识,授人以鱼不如授人以渔,如果对投资感兴趣的人,我相信你会觉得相见恨晚。
老齐的读书圈:里面包含了几百本书籍,主要包括投资类,格局类,经营管理类三种。感兴趣的可以听听,三天内无条件可以退款。强烈推荐
齐俊杰的粉丝群:主要是对市场的解读,当然里面主要考虑的是周期的定位以及资产配置的相关知识。感兴趣的也可以进来听听,