Spring版本
5.2.5.RELEASE
參考
源碼解讀
在Spring源碼中奖年,bean標簽的解析交由processBeanDefinition處理:
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
// 解析bean,如果解析成功植捎,獲得持有name和alias的BeanDefinitionHolder狡蝶,即BeanDefinitionHolder對象咸这,解析失敗返回null
BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
if (bdHolder != null) {
// bean支持定義屬性和內(nèi)部嵌套自定義標簽,進行這一部分的處理
bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
try {
// 注冊bean
// Register the final decorated instance.
BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
}
catch (BeanDefinitionStoreException ex) {
getReaderContext().error("Failed to register bean definition with name '" +
bdHolder.getBeanName() + "'", ele, ex);
}
// 通知監(jiān)聽器注冊事件
// Send registration event.
getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
}
}
該方法主要做了以下三點:
- 解析bean標簽
- 解析bean標簽的自定義屬性和bean標簽下嵌套的自定義標簽
- 注冊bean拱雏,并且在注冊成功之后通知監(jiān)聽器注冊事件
1. parseBeanDefinitionElement
@Nullable
public BeanDefinitionHolder parseBeanDefinitionElement(Element ele) {
return parseBeanDefinitionElement(ele, null);
}
繼續(xù)交由重載方法:
@Nullable
public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, @Nullable BeanDefinition containingBean) {
String id = ele.getAttribute(ID_ATTRIBUTE);
String nameAttr = ele.getAttribute(NAME_ATTRIBUTE);
// 將name屬性值進行切割棉安,塞進別名集合
List<String> aliases = new ArrayList<>();
if (StringUtils.hasLength(nameAttr)) {
String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS);
aliases.addAll(Arrays.asList(nameArr));
}
// 如果沒有聲明id屬性,將上面解析出來的第一個別名當成beanName
String beanName = id;
if (!StringUtils.hasText(beanName) && !aliases.isEmpty()) {
beanName = aliases.remove(0);
if (logger.isTraceEnabled()) {
logger.trace("No XML 'id' specified - using '" + beanName +
"' as bean name and " + aliases + " as aliases");
}
}
if (containingBean == null) {
// 檢查beanName和aliases中的元素是否唯一
checkNameUniqueness(beanName, aliases, ele);
}
AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean);
if (beanDefinition != null) {
if (!StringUtils.hasText(beanName)) {
try {
if (containingBean != null) {
// 生成唯一beanName
beanName = BeanDefinitionReaderUtils.generateBeanName(
beanDefinition, this.readerContext.getRegistry(), true);
}
else {
// 生成唯一beanName
beanName = this.readerContext.generateBeanName(beanDefinition);
// Register an alias for the plain bean class name, if still possible,
// if the generator returned the class name plus a suffix.
// This is expected for Spring 1.2/2.0 backwards compatibility.
String beanClassName = beanDefinition.getBeanClassName();
if (beanClassName != null &&
beanName.startsWith(beanClassName) && beanName.length() > beanClassName.length() &&
!this.readerContext.getRegistry().isBeanNameInUse(beanClassName)) {
aliases.add(beanClassName);
}
}
if (logger.isTraceEnabled()) {
logger.trace("Neither XML 'id' nor 'name' specified - " +
"using generated bean name [" + beanName + "]");
}
}
catch (Exception ex) {
error(ex.getMessage(), ele);
return null;
}
}
String[] aliasesArray = StringUtils.toStringArray(aliases);
// 構(gòu)建BeanDefinitionHolder對象铸抑,返回
return new BeanDefinitionHolder(beanDefinition, beanName, aliasesArray);
}
return null;
}
邏輯在于構(gòu)建一個包含beanDefinition贡耽、beanName和aliasesArray的BeanDefinitionHolder對象,因此鹊汛,整個過程也就可以拆分成三個部分來查看:
- 生成aliasesArray
- 解析獲取beanDefinition對象
- 生成beanName
1.1 生成aliasesArray
// 將name屬性值進行切割蒲赂,塞進別名集合
List<String> aliases = new ArrayList<>();
if (StringUtils.hasLength(nameAttr)) {
String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS);
aliases.addAll(Arrays.asList(nameArr));
}
獲取到name屬性的值,之后切割并組裝成List類型的aliases刁憋,在構(gòu)建BeanDefinitionHolder的時候滥嘴,將List類型的aliases轉(zhuǎn)化為數(shù)組類型并作為構(gòu)造參數(shù):
String[] aliasesArray = StringUtils.toStringArray(aliases);
1.2 解析獲取beanDefinition對象
核心方法在于:
AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean);
查看parseBeanDefinitionElement方法:
@Nullable
public AbstractBeanDefinition parseBeanDefinitionElement(
Element ele, String beanName, @Nullable BeanDefinition containingBean) {
// ParseState的定義生命信息如下:
// Simple {@link LinkedList}-based structure for tracking the logical position during a parsing process.
// 看起來,parseState是用于追蹤bean解析過程中的邏輯位置
// 追蹤parseState的使用方式至耻,應該是解析過程報錯的情況下可以輕易找到當前正在被解析的bean吧
this.parseState.push(new BeanEntry(beanName));
String className = null;
if (ele.hasAttribute(CLASS_ATTRIBUTE)) {
className = ele.getAttribute(CLASS_ATTRIBUTE).trim();
}
String parent = null;
if (ele.hasAttribute(PARENT_ATTRIBUTE)) {
parent = ele.getAttribute(PARENT_ATTRIBUTE);
}
try {
// createBeanDefinition用于創(chuàng)建一個AbstractBeanDefinition對象若皱,且將className進行加載,設置進該對象的beanClass屬性尘颓,同時將parent設置進去parentName屬性
AbstractBeanDefinition bd = createBeanDefinition(className, parent);
// 解析bean的各種屬性走触,比如autowire,lazyinit疤苹,設置到bd中
parseBeanDefinitionAttributes(ele, beanName, containingBean, bd);
// 解析description子元素內(nèi)容互广,getChildElementValueByTagName可以從ele中找到符合DESCRIPTION_ELEMENT的第一個子元素的內(nèi)容文本值
bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION_ELEMENT));
// 解析meta子元素
parseMetaElements(ele, bd);
// 解析lookup-method子元素
parseLookupOverrideSubElements(ele, bd.getMethodOverrides());
// 解析replace-method子元素
parseReplacedMethodSubElements(ele, bd.getMethodOverrides());
// 解析construct-arg子元素
parseConstructorArgElements(ele, bd);
// 解析property子元素
parsePropertyElements(ele, bd);
// 解析qualifier子元素
parseQualifierElements(ele, bd);
bd.setResource(this.readerContext.getResource());
bd.setSource(extractSource(ele));
return bd;
}
catch (ClassNotFoundException ex) {
error("Bean class [" + className + "] not found", ele, ex);
}
catch (NoClassDefFoundError err) {
error("Class that bean class [" + className + "] depends on not found", ele, err);
}
catch (Throwable ex) {
error("Unexpected failure during bean definition parsing", ele, ex);
}
finally {
this.parseState.pop();
}
return null;
}
邏輯可以歸納為以下幾個步驟:
- 將當前解析的beanName壓入parseState棧中,方便解析過程報錯的時候定位到當前被解析元素
- 獲取class屬性和parent屬性痰催,調(diào)用createBeanDefinition方法構(gòu)建AbstractBeanDefinition對象兜辞,在調(diào)用createBeanDefinition方法的時候,對className進行了加載
- 解析bean標簽的屬性
- 解析bean標簽下的子標簽
- 解析完畢夸溶,從parseState中退棧
1.2.1 createBeanDefinition
public static AbstractBeanDefinition createBeanDefinition(
@Nullable String parentName, @Nullable String className, @Nullable ClassLoader classLoader) throws ClassNotFoundException {
GenericBeanDefinition bd = new GenericBeanDefinition();
bd.setParentName(parentName);
if (className != null) {
if (classLoader != null) {
bd.setBeanClass(ClassUtils.forName(className, classLoader));
}
else {
bd.setBeanClassName(className);
}
}
return bd;
}
可以看到逸吵,該方法構(gòu)建一個GenericBeanDefinition對象,之后如果className和classLoader不為空缝裁,則加載類扫皱,并設置為beanClass屬性足绅,否則將className設置進beanClassName屬性
1.2.2 parseBeanDefinitionAttributes
解析bean標簽的眾多屬性,包含:
- scope
- abstract
- lazy-init
- autowire
- depends-on
- autowire-candidate
- primary
- init-method
- destroy-method
- factory-method
- factory-bean
代碼邏輯較為簡單韩脑,不贅述
1.2.3 生成beanName
首先獲取id屬性作為beanName:
String beanName = id;
如果沒有生命id屬性氢妈,則使用別名的第一個元素作為beanName:
if (!StringUtils.hasText(beanName) && !aliases.isEmpty()) {
beanName = aliases.remove(0);
if (logger.isTraceEnabled()) {
logger.trace("No XML 'id' specified - using '" + beanName +
"' as bean name and " + aliases + " as aliases");
}
}
在上述beanName的基礎上生成一個唯一的beanName:
if (containingBean != null) {
// 生成唯一beanName
beanName = BeanDefinitionReaderUtils.generateBeanName(
beanDefinition, this.readerContext.getRegistry(), true);
}
else {
// 生成唯一beanName
beanName = this.readerContext.generateBeanName(beanDefinition);
// Register an alias for the plain bean class name, if still possible,
// if the generator returned the class name plus a suffix.
// This is expected for Spring 1.2/2.0 backwards compatibility.
String beanClassName = beanDefinition.getBeanClassName();
if (beanClassName != null &&
beanName.startsWith(beanClassName) && beanName.length() > beanClassName.length() &&
!this.readerContext.getRegistry().isBeanNameInUse(beanClassName)) {
aliases.add(beanClassName);
}
}
2. decorateBeanDefinitionIfRequired
bean標簽支持自定義屬性,我們簡單使用一個demo演示該部分內(nèi)容:
2.1 demo
2.1.1 Student
public class Student {
private String id;
private String name;
private String desc;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
2.1.2 student.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.customAttribute.com/schema/student-desc" targetNamespace="http://www.customAttribute.com/schema/student-desc" elementFormDefault="qualified">
<xsd:attribute name="student-desc" type="xsd:string"/>
</xsd:schema>
2.1.3 StudentNamespaceHandler
public class StudentNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionDecoratorForAttribute("student-desc", new StudentBeanDefinitionDecorator());
}
}
2.1.4 StudentBeanDefinitionDecorator
public class StudentBeanDefinitionDecorator implements BeanDefinitionDecorator {
@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
definition.getBeanDefinition().getPropertyValues().add("desc", ((Attr)node).getValue());
return definition;
}
}
2.1.5 spring.handlers
http\://www.customAttribute.com/schema/student-desc=com.kungyu.custom.element.StudentNamespaceHandler
2.1.6 spring.schemas
http\://www.customAttribute.com/schema/student.xsd=META-INF/student.xsd
2.1.7 spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:student="http://www.customAttribute.com/schema/student-desc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.kungyu.custom.element.Student" student:student-desc="student desc">
<property name="id" value="id"/>
<property name="name" value="name"/>
</bean>
</beans>
2.1.8 測試
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getId() + "----" + student.getName() + "---" + student.getDesc());
}
}
2.1.9 結(jié)果
在
spring.xml
中段多,我們并沒有為id
為student
的bean
指定desc
屬性的值首量,而是通過自定義屬性student-desc
进苍,之后交由在StudentNamespaceHandler
中注冊的StudentBeanDefinitionDecorator
進行處理加缘,在StudentBeanDefinitionDecorator
的decorate
中給BeanDefinition
定義student
的desc
屬性的值。
2.2 源碼解讀
public BeanDefinitionHolder decorateBeanDefinitionIfRequired(
Element ele, BeanDefinitionHolder originalDef, @Nullable BeanDefinition containingBd) {
BeanDefinitionHolder finalDefinition = originalDef;
// 解析bean標簽里面的自定義屬性
// Decorate based on custom attributes first.
NamedNodeMap attributes = ele.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Node node = attributes.item(i);
finalDefinition = decorateIfRequired(node, finalDefinition, containingBd);
}
// 解析嵌套在bean標簽里面的自定義標簽
// Decorate based on custom nested elements.
NodeList children = ele.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node node = children.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
finalDefinition = decorateIfRequired(node, finalDefinition, containingBd);
}
}
return finalDefinition;
}
倆個步驟:
- 解析bean標簽的自定義屬性
- 解析bean標簽內(nèi)部嵌套的自定義標簽
以上倆個步驟都通過decorateIfRequired方法實現(xiàn):
public BeanDefinitionHolder decorateIfRequired(
Node node, BeanDefinitionHolder originalDef, @Nullable BeanDefinition containingBd) {
String namespaceUri = getNamespaceURI(node);
if (namespaceUri != null && !isDefaultNamespace(namespaceUri)) {
// 同解析自定義標簽一樣觉啊,獲取自定義的handler
NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
if (handler != null) {
// 調(diào)用自定義handler的decorate方法
BeanDefinitionHolder decorated =
handler.decorate(node, originalDef, new ParserContext(this.readerContext, this, containingBd));
if (decorated != null) {
return decorated;
}
}
else if (namespaceUri.startsWith("http://www.springframework.org/schema/")) {
error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", node);
}
else {
// A custom namespace, not to be handled by Spring - maybe "xml:...".
if (logger.isDebugEnabled()) {
logger.debug("No Spring NamespaceHandler found for XML schema namespace [" + namespaceUri + "]");
}
}
}
return originalDef;
}
該方法很明顯邏輯在于:
- 查找到自定義的handler
- 通過handler拣宏,調(diào)用自定義的decorate方法
3. registerBeanDefinition
public static void registerBeanDefinition(
BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
throws BeanDefinitionStoreException {
// Register bean definition under primary name.
String beanName = definitionHolder.getBeanName();
// 注冊,將beanName和beanDefinition寫入緩存map中
registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());
// 別名注冊
// Register aliases for bean name, if any.
String[] aliases = definitionHolder.getAliases();
if (aliases != null) {
for (String alias : aliases) {
registry.registerAlias(beanName, alias);
}
}
}
邏輯都比較簡單杠人,就直接貼代碼了
3.1 registerBeanDefinition
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException {
Assert.hasText(beanName, "Bean name must not be empty");
Assert.notNull(beanDefinition, "BeanDefinition must not be null");
if (beanDefinition instanceof AbstractBeanDefinition) {
try {
// 對methodOverrides進行校驗
((AbstractBeanDefinition) beanDefinition).validate();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
"Validation of bean definition failed", ex);
}
}
// 獲取緩存
BeanDefinition existingDefinition = this.beanDefinitionMap.get(beanName);
// 如果緩存存在
if (existingDefinition != null) {
// 如果不允許BeanDefinition覆蓋勋乾,拋出異常
if (!isAllowBeanDefinitionOverriding()) {
throw new BeanDefinitionOverrideException(beanName, beanDefinition, existingDefinition);
}
else if (existingDefinition.getRole() < beanDefinition.getRole()) {
// 如果緩存的BeanDefinition角色比注冊的BeanDefinition的角色d低
// e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
if (logger.isInfoEnabled()) {
logger.info("Overriding user-defined bean definition for bean '" + beanName +
"' with a framework-generated bean definition: replacing [" +
existingDefinition + "] with [" + beanDefinition + "]");
}
}
else if (!beanDefinition.equals(existingDefinition)) {
// 如果倆個BeanDefinition不同
if (logger.isDebugEnabled()) {
logger.debug("Overriding bean definition for bean '" + beanName +
"' with a different definition: replacing [" + existingDefinition +
"] with [" + beanDefinition + "]");
}
}
else {
if (logger.isTraceEnabled()) {
logger.trace("Overriding bean definition for bean '" + beanName +
"' with an equivalent definition: replacing [" + existingDefinition +
"] with [" + beanDefinition + "]");
}
}
// 進行覆蓋
this.beanDefinitionMap.put(beanName, beanDefinition);
}
else {
// 如果不存在緩存
// 判斷是否已經(jīng)開始Bean創(chuàng)建的進程
if (hasBeanCreationStarted()) {
// 如果已開始,進行同步控制
// Cannot modify startup-time collection elements anymore (for stable iteration)
synchronized (this.beanDefinitionMap) {
// 寫入緩存
this.beanDefinitionMap.put(beanName, beanDefinition);
// 將beanName加入beanDefinitionNames
List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1);
updatedDefinitions.addAll(this.beanDefinitionNames);
updatedDefinitions.add(beanName);
this.beanDefinitionNames = updatedDefinitions;
removeManualSingletonName(beanName);
}
}
else {
// Still in startup registration phase
this.beanDefinitionMap.put(beanName, beanDefinition);
this.beanDefinitionNames.add(beanName);
removeManualSingletonName(beanName);
}
this.frozenBeanDefinitionNames = null;
}
if (existingDefinition != null || containsSingleton(beanName)) {
resetBeanDefinition(beanName);
}
}
核心其實就是將BeanDefinition寫入緩存中:
this.beanDefinitionMap.put(beanName, beanDefinition);
3.2 registerAlias
該方法在《Spring源碼解析(五)-解析alias標簽》已經(jīng)講述過了嗡善,感興趣可以戳