玖叶教程网

前端编程开发入门

万字长文带你阅读Spring源码之一Bean定义对象

本系列文章基于Spring5.x版本。不同版本源码可能会有不同。


Bean定义

在学习Spring源码之前,首先需要了解到Spring框架内部对于bean的定义结构。Spring框架会在bean实例化、初始化的过程中反复用到这些定义信息。


Bean定义对象包括如下几个属性:


  • bean实例,https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-class
  • Bean名称,https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-beanname
  • Bean类型(单例、原型..),https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-scopes
  • 构造函数参数 | [Dependency Injection],https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-collaborators)
  • Properties | [Dependency Injection],https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-collaborators
  • 自动注入模型,https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-autowire
  • 懒加载模型,https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-lazy-init
  • 初始化方法,https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-lifecycle-initializingbean
  • 销毁方法,https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-lifecycle-disposablebean


bean定义所有属性大概有上百个。就以最常用的RootBeanDefinition为例,这个类是bean定义最核心的实现类。类中定义非常多属性,这些属性都是bean的定义信息。

/** Map with String keys and Object values. */
private final Map<String, Object> attributes = new LinkedHashMap<>();

@Nullable
private Object source;

/**
	 * Constant for the default scope name: {@code ""}, equivalent to singleton
	 * status unless overridden from a parent bean definition (if applicable).
	 * 默认bean作用域
	 */
public static final String SCOPE_DEFAULT = "";

/**
	 * Constant that indicates no external autowiring at all.
	 * 自动注入模式
	 * @see #setAutowireMode
	 */
public static final int AUTOWIRE_NO = AutowireCapableBeanFactory.AUTOWIRE_NO;

/**
	 * Constant that indicates autowiring bean properties by name.
	 * 自动注入通过bean名字
	 * @see #setAutowireMode
	 */
public static final int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;

/**
	 * Constant that indicates autowiring bean properties by type.
	 * 自动注入通过bean类型
	 * @see #setAutowireMode
	 */
public static final int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;

/**
	 * Constant that indicates autowiring a constructor.
	 * 自动注入通过构造函数
	 * @see #setAutowireMode
	 */
public static final int AUTOWIRE_CONSTRUCTOR = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;

/**
	 * Constant that indicates determining an appropriate autowire strategy
	 * through introspection of the bean class.
	 * @see #setAutowireMode
	 * @deprecated as of Spring 3.0: If you are using mixed autowiring strategies,
	 * use annotation-based autowiring for clearer demarcation of autowiring needs.
	 */
@Deprecated
public static final int AUTOWIRE_AUTODETECT = AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT;

/**
	 * Constant that indicates no dependency check at all.
	 * @see #setDependencyCheck
	 */
public static final int DEPENDENCY_CHECK_NONE = 0;

/**
	 * Constant that indicates dependency checking for object references.
	 * @see #setDependencyCheck
	 */
public static final int DEPENDENCY_CHECK_OBJECTS = 1;

/**
	 * Constant that indicates dependency checking for "simple" properties.
	 * @see #setDependencyCheck
	 * @see org.springframework.beans.BeanUtils#isSimpleProperty
	 */
public static final int DEPENDENCY_CHECK_SIMPLE = 2;

/**
	 * Constant that indicates dependency checking for all properties
	 * (object references as well as "simple" properties).
	 * @see #setDependencyCheck
	 */
public static final int DEPENDENCY_CHECK_ALL = 3;

/**
	 * Constant that indicates the container should attempt to infer the
	 * {@link #setDestroyMethodName destroy method name} for a bean as opposed to
	 * explicit specification of a method name. The value {@value} is specifically
	 * designed to include characters otherwise illegal in a method name, ensuring
	 * no possibility of collisions with legitimately named methods having the same
	 * name.
	 * <p>Currently, the method names detected during destroy method inference
	 * are "close" and "shutdown", if present on the specific bean class.
	 */
public static final String INFER_METHOD = "(inferred)";


@Nullable
private volatile Object beanClass;

@Nullable
private String scope = SCOPE_DEFAULT;

private boolean abstractFlag = false;

@Nullable
private Boolean lazyInit;

private int autowireMode = AUTOWIRE_NO;

private int dependencyCheck = DEPENDENCY_CHECK_NONE;

@Nullable
private String[] dependsOn;

private boolean autowireCandidate = true;

private boolean primary = false;

private final Map<String, AutowireCandidateQualifier> qualifiers = new LinkedHashMap<>();

@Nullable
private Supplier<?> instanceSupplier;

private boolean nonPublicAccessAllowed = true;

private boolean lenientConstructorResolution = true;

@Nullable
private String factoryBeanName;

@Nullable
private String factoryMethodName;

@Nullable
private ConstructorArgumentValues constructorArgumentValues;

@Nullable
private MutablePropertyValues propertyValues;

private MethodOverrides methodOverrides = new MethodOverrides();

@Nullable
private String initMethodName;

@Nullable
private String destroyMethodName;

private boolean enforceInitMethod = true;

private boolean enforceDestroyMethod = true;

private boolean synthetic = false;

private int role = BeanDefinition.ROLE_APPLICATION;

@Nullable
private String description;

@Nullable
private Resource resource;

@Nullable
private BeanDefinitionHolder decoratedDefinition;

@Nullable
private AnnotatedElement qualifiedElement;

/** Determines if the definition needs to be re-merged. */
volatile boolean stale;

boolean allowCaching = true;

boolean isFactoryMethodUnique = false;

@Nullable
volatile ResolvableType targetType;

/** Package-visible field for caching the determined Class of a given bean definition. */
@Nullable
volatile Class<?> resolvedTargetType;

/** Package-visible field for caching if the bean is a factory bean. */
@Nullable
volatile Boolean isFactoryBean;

/** Package-visible field for caching the return type of a generically typed factory method. */
@Nullable
volatile ResolvableType factoryMethodReturnType;

/** Package-visible field for caching a unique factory method candidate for introspection. */
@Nullable
volatile Method factoryMethodToIntrospect;

/** Common lock for the four constructor fields below. */
final Object constructorArgumentLock = new Object();

/** Package-visible field for caching the resolved constructor or factory method. */
@Nullable
Executable resolvedConstructorOrFactoryMethod;

/** Package-visible field that marks the constructor arguments as resolved. */
boolean constructorArgumentsResolved = false;

/** Package-visible field for caching fully resolved constructor arguments. */
@Nullable
Object[] resolvedConstructorArguments;

/** Package-visible field for caching partly prepared constructor arguments. */
@Nullable
Object[] preparedConstructorArguments;

/** Common lock for the two post-processing fields below. */
final Object postProcessingLock = new Object();

/** Package-visible field that indicates MergedBeanDefinitionPostProcessor having been applied. */
boolean postProcessed = false;

/** Package-visible field that indicates a before-instantiation post-processor having kicked in. */
@Nullable
volatile Boolean beforeInstantiationResolved;

@Nullable
private Set<Member> externallyManagedConfigMembers;

@Nullable
private Set<String> externallyManagedInitMethods;

@Nullable
private Set<String> externallyManagedDestroyMethods;

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言