这篇文章主要介绍了Spring源码之BeanDefinition类是什么,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。
Spring版本为5.1.5
用过spring的人都知道,我们将对象注入到spring容器中,交给spring来帮我们管理。这种对象我们称之为bean对象。但是这些bean对象在spring容器中,到底是以什么形式存在,具有哪些属性、行为呢?今天我们进入到spring源码来一探究竟。
bean的创建工厂BeanFactory有个默认实现类DefaultListableBeanFactory,内部有个存放所有注入bean对象信息的Map
/** Map of bean definition objects, keyed by bean name. */ private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
Map的value对象BeanDefinition就是spring中对bean的定义和描述,具体概述如下:
属性 | 行为 | 解释 |
---|---|---|
parentName | String getParentName(); void setParentName(@Nullable String parentName); | bean定义对象的父类定义对象名称 |
beanClassName | String getBeanClassName(); void setBeanClassName(@Nullable String beanClassName); | bean对象的实际class类 |
scope | String getScope(); void setScope(@Nullable String scope); | bean对象是否为单例<singleton或者prototype> |
lazyInit | boolean isLazyInit(); void setLazyInit(boolean lazyInit); | 是否懒加载 |
dependsOn | String[] getDependsOn(); void setDependsOn(@Nullable String... dependsOn); | 设置依赖的bean对象,被依赖的bean对象总是会比当前bean对象先创建 |
autowireCandidate | boolean isAutowireCandidate(); void setAutowireCandidate(boolean autowireCandidate); | 设置是否可以自动注入。只对@Autowired注解有效,配置文件中可以通过property显示注入 |
primary | boolean isPrimary(); void setPrimary(boolean primary); | 配置bean为主要候选bean。当同一个接口的多个实现类或者一个类多次注入到spring容器时,通过该属性来配置某个bean为主候选bean,通过类型来注入时,默认为使用主候选bean注入 |
factoryBeanName | String getFactoryBeanName(); void setFactoryBeanName(@Nullable String factoryBeanName); | 设置创建bean的工厂名称 |
factoryMethodName | String getFactoryMethodName(); void setFactoryMethodName(@Nullable String factoryMethodName); | 设置创建bean的工厂中,创建bean的具体方法 |
initMethodName | String getInitMethodName(); void setInitMethodName(@Nullable String initMethodName); | 设置创建bean时,默认初始化的方法 |
destroyMethodName | String getDestroyMethodName(); void setDestroyMethodName(@Nullable String destroyMethodName); | 设置销毁bean时调用的方法名称。注意需要调用context的close()方法才会调用 |
role | int getRole(); void setRole(int role); | 设置bean的分类 |
description | String getDescription(); void setDescription(@Nullable String description); | 对bean对象的描述 |
注意:BeanDefinition是一个接口,内部只定义了bean对象的一些基本行为。上表中的属性在BeanDefinition中并不是真实存在的,只是通过set、get方法来设置和获取。以下抽取出部分BeanDefinition源代码供大家感知下
public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { /** * Override the target scope of this bean, specifying a new scope name. * @see #SCOPE_SINGLETON * @see #SCOPE_PROTOTYPE */ void setScope(@Nullable String scope); /** * Return the name of the current target scope for this bean, * or {@code null} if not known yet. */ @Nullable String getScope(); }
假设有以下两个bean,Java代码如下
MyTestBean
package com.yuanweiquan.learn.bean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Description; public class MyTestBean { @Autowired private AutowireCandidateBean autowireCandidateBean; public void init() { System.out.println("inti MyTestBean"); } public AutowireCandidateBean getAutowireCandidateBean() { return autowireCandidateBean; } public void setAutowireCandidateBean(AutowireCandidateBean bean) { this.autowireCandidateBean = bean; } }
AutowireCandidateBean
package com.yuanweiquan.learn.bean; public class AutowireCandidateBean { public void initBean() { System.out.println("init AutowireCandidateBean"); } public void destroyBean() { System.out.println("destroy AutowireCandidateBean"); } }
下面看如何在配置文件applicationContext.xml来进行配置
<bean id="myTestBean" class="com.yuanweiquan.learn.bean.MyTestBean" depends-on="autowireCandidateBean" init-method="init"/> <bean id="autowireCandidateBean" class="com.yuanweiquan.learn.bean.AutowireCandidateBean" init-method="initBean" autowire-candidate="true" destroy-method="destroyBean" scope="singleton" parent="myTestBean" lazy-init="default" primary="true"> <description>autowireCandidateBean description</description> </bean>
接下来就是测试代码
FileSystemXmlApplicationContext factory = new FileSystemXmlApplicationContext("classpath:applicationContext.xml"); BeanDefinition myTestBeanDefinition = factory.getBeanFactory().getBeanDefinition("autowireCandidateBean"); //输出 System.out.println("bean description:" + myTestBeanDefinition.getDescription()); System.out.println("bean class name:" + myTestBeanDefinition.getBeanClassName()); System.out.println("parent name:" + myTestBeanDefinition.getParentName()); System.out.println("scope:" + myTestBeanDefinition.getScope()); System.out.println("is lazyinit:" + myTestBeanDefinition.isLazyInit()); System.out.println("depends On:" + myTestBeanDefinition.getDependsOn()); System.out.println("is autowireCandidate:" + myTestBeanDefinition.isAutowireCandidate()); System.out.println("is primary:" + myTestBeanDefinition.isPrimary()); System.out.println("factory bean name:"+myTestBeanDefinition.getFactoryBeanName()); System.out.println("factory bean method name:" + myTestBeanDefinition.getFactoryMethodName()); System.out.println("init method name:" + myTestBeanDefinition.getInitMethodName()); System.out.println("destory method name:" + myTestBeanDefinition.getDestroyMethodName()); System.out.println("role:" + myTestBeanDefinition.getRole()); //关闭context,否则不会调用bean的销毁方法 factory.close();
控制台输出如下
init AutowireCandidateBean inti MyTestBean bean description:autowireCandidateBean description bean class name:com.yuanweiquan.learn.bean.AutowireCandidateBean parent name:myTestBean scope:singleton is lazyinit:false depends On:null is autowireCandidate:true is primary:true factory bean name:null factory bean method name:null init method name:initBean destory method name:destroyBean role:0 destroy AutowireCandidateBean
感谢你能够认真阅读完这篇文章,希望小编分享Spring源码之BeanDefinition类是什么内容对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。