001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.context.annotation; 018 019import java.util.Set; 020 021import org.w3c.dom.Element; 022 023import org.springframework.beans.factory.config.BeanDefinition; 024import org.springframework.beans.factory.config.BeanDefinitionHolder; 025import org.springframework.beans.factory.parsing.BeanComponentDefinition; 026import org.springframework.beans.factory.parsing.CompositeComponentDefinition; 027import org.springframework.beans.factory.xml.BeanDefinitionParser; 028import org.springframework.beans.factory.xml.ParserContext; 029import org.springframework.lang.Nullable; 030 031/** 032 * Parser for the <context:annotation-config/> element. 033 * 034 * @author Mark Fisher 035 * @author Juergen Hoeller 036 * @author Christian Dupuis 037 * @since 2.5 038 * @see AnnotationConfigUtils 039 */ 040public class AnnotationConfigBeanDefinitionParser implements BeanDefinitionParser { 041 042 @Override 043 @Nullable 044 public BeanDefinition parse(Element element, ParserContext parserContext) { 045 Object source = parserContext.extractSource(element); 046 047 // Obtain bean definitions for all relevant BeanPostProcessors. 048 Set<BeanDefinitionHolder> processorDefinitions = 049 AnnotationConfigUtils.registerAnnotationConfigProcessors(parserContext.getRegistry(), source); 050 051 // Register component for the surrounding <context:annotation-config> element. 052 CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source); 053 parserContext.pushContainingComponent(compDefinition); 054 055 // Nest the concrete beans in the surrounding component. 056 for (BeanDefinitionHolder processorDefinition : processorDefinitions) { 057 parserContext.registerComponent(new BeanComponentDefinition(processorDefinition)); 058 } 059 060 // Finally register the composite component. 061 parserContext.popAndRegisterContainingComponent(); 062 063 return null; 064 } 065 066}