曹工说Spring Boot源码系列开讲了(1)-- Bean Definition到底是什么,附spring思维导图分享 (2)

实话说,是可以的,一些简单的,轻量级ioc容器就是这么玩的,但是spring作为优秀代码的代表,肯定不能这么low,接口的抽象性要好得多,方便我们替换不同的实现,该用接口来抽象,肯定要抽象为接口。

下边我们就看看该接口,先看接口的描述:

* A BeanDefinition describes a bean instance, which has property values, * constructor argument values, and further information supplied by * concrete implementations. * * <p>This is just a minimal interface: The main intention is to allow a * {@link BeanFactoryPostProcessor} such as {@link PropertyPlaceholderConfigurer} * to introspect and modify property values and other bean metadata. * @author Juergen Hoeller * @author Rob Harrop * @since 19.03.2004

这里说的是,bean definition描述一个bean实例的各种属性,尤其声明了:这是一个最小化接口,主要目的是允许bean factory后置处理器,对bean property和其他元数据进行修改。而且,这是2004年的接口,可想而知,是多么核心的api了。

再看看具体定义的方法,更好理解:

public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { /** * Return the name of the parent definition of this bean definition, if any. */ String getParentName(); /** * Set the name of the parent definition of this bean definition, if any. */ void setParentName(String parentName); /** * Return the current bean class name of this bean definition. * <p>Note that this does not have to be the actual class name used at runtime, in * case of a child definition overriding/inheriting the class name from its parent. * Hence, do <i>not</i> consider this to be the definitive bean type at runtime but * rather only use it for parsing purposes at the individual bean definition level. */ String getBeanClassName(); /** * Override the bean class name of this bean definition. * <p>The class name can be modified during bean factory post-processing, * typically replacing the original class name with a parsed variant of it. */ void setBeanClassName(String beanClassName); /** * Return the factory bean name, if any. */ String getFactoryBeanName(); /** * Specify the factory bean to use, if any. */ void setFactoryBeanName(String factoryBeanName); /** * Return a factory method, if any. */ String getFactoryMethodName(); /** * Specify a factory method, if any. This method will be invoked with * constructor arguments, or with no arguments if none are specified. * The method will be invoked on the specified factory bean, if any, * or otherwise as a static method on the local bean class. * @param factoryMethodName static factory method name, * or {@code null} if normal constructor creation should be used * @see #getBeanClassName() */ void setFactoryMethodName(String factoryMethodName); /** * Return the name of the current target scope for this bean, * or {@code null} if not known yet. */ String getScope(); /** * Override the target scope of this bean, specifying a new scope name. * @see #SCOPE_SINGLETON * @see #SCOPE_PROTOTYPE */ void setScope(String scope); /** * Return whether this bean should be lazily initialized, i.e. not * eagerly instantiated on startup. Only applicable to a singleton bean. */ boolean isLazyInit(); /** * Set whether this bean should be lazily initialized. * <p>If {@code false}, the bean will get instantiated on startup by bean * factories that perform eager initialization of singletons. */ void setLazyInit(boolean lazyInit); /** * Return the bean names that this bean depends on. */ String[] getDependsOn(); /** * Set the names of the beans that this bean depends on being initialized. * The bean factory will guarantee that these beans get initialized first. */ void setDependsOn(String[] dependsOn); /** * Return whether this bean is a candidate for getting autowired into some other bean. */ boolean isAutowireCandidate(); /** * Set whether this bean is a candidate for getting autowired into some other bean. */ void setAutowireCandidate(boolean autowireCandidate); /** * Return whether this bean is a primary autowire candidate. * If this value is true for exactly one bean among multiple * matching candidates, it will serve as a tie-breaker. */ boolean isPrimary(); /** * Set whether this bean is a primary autowire candidate. * <p>If this value is true for exactly one bean among multiple * matching candidates, it will serve as a tie-breaker. */ void setPrimary(boolean primary); /** * Return the constructor argument values for this bean. * <p>The returned instance can be modified during bean factory post-processing. * @return the ConstructorArgumentValues object (never {@code null}) */ ConstructorArgumentValues getConstructorArgumentValues(); /** * Return the property values to be applied to a new instance of the bean. * <p>The returned instance can be modified during bean factory post-processing. * @return the MutablePropertyValues object (never {@code null}) */ MutablePropertyValues getPropertyValues(); /** * Return whether this a <b>Singleton</b>, with a single, shared instance * returned on all calls. * @see #SCOPE_SINGLETON */ boolean isSingleton(); /** * Return whether this a <b>Prototype</b>, with an independent instance * returned for each call. * @see #SCOPE_PROTOTYPE */ boolean isPrototype(); /** * Return whether this bean is "abstract", that is, not meant to be instantiated. */ boolean isAbstract(); /** * Get the role hint for this {@code BeanDefinition}. The role hint * provides tools with an indication of the importance of a particular * {@code BeanDefinition}. * @see #ROLE_APPLICATION * @see #ROLE_INFRASTRUCTURE * @see #ROLE_SUPPORT */ int getRole(); /** * Return a human-readable description of this bean definition. */ String getDescription(); /** * Return a description of the resource that this bean definition * came from (for the purpose of showing context in case of errors). */ String getResourceDescription(); /** * Return the originating BeanDefinition, or {@code null} if none. * Allows for retrieving the decorated bean definition, if any. * <p>Note that this method returns the immediate originator. Iterate through the * originator chain to find the original BeanDefinition as defined by the user. */ BeanDefinition getOriginatingBeanDefinition(); }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpzfpg.html