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; 029 030/** 031 * Parser for the <context:annotation-config/> element. 032 * 033 * @author Mark Fisher 034 * @author Juergen Hoeller 035 * @author Christian Dupuis 036 * @since 2.5 037 * @see AnnotationConfigUtils 038 */ 039public class AnnotationConfigBeanDefinitionParser implements BeanDefinitionParser { 040 041 @Override 042 public BeanDefinition parse(Element element, ParserContext parserContext) { 043 Object source = parserContext.extractSource(element); 044 045 // Obtain bean definitions for all relevant BeanPostProcessors. 046 Set<BeanDefinitionHolder> processorDefinitions = 047 AnnotationConfigUtils.registerAnnotationConfigProcessors(parserContext.getRegistry(), source); 048 049 // Register component for the surrounding <context:annotation-config> element. 050 CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source); 051 parserContext.pushContainingComponent(compDefinition); 052 053 // Nest the concrete beans in the surrounding component. 054 for (BeanDefinitionHolder processorDefinition : processorDefinitions) { 055 parserContext.registerComponent(new BeanComponentDefinition(processorDefinition)); 056 } 057 058 // Finally register the composite component. 059 parserContext.popAndRegisterContainingComponent(); 060 061 return null; 062 } 063 064}