@ -1,47 +1,108 @@
@@ -1,47 +1,108 @@
/ *
* Copyright 2002 - 2009 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
package org.springframework.config.java.support ;
import org.springframework.beans.factory.config.BeanDefinition ;
import org.springframework.beans.factory.support.BeanDefinitionRegistry ;
import org.springframework.config.java.Configuration ;
import org.springframework.config.java.ConfigurationModel ;
import org.springframework.config.java.MalformedConfigurationException ;
import org.springframework.config.java.ext.Bean ;
import org.springframework.config.java.internal.parsing.ConfigurationParser ;
/ * *
* Abstract superclass for processing { @link Configuration } - annotated classes and registering
* bean definitions based on { @link Bean } - annotated methods within those classes .
*
* < p > Provides template method { @link # processConfigBeanDefinitions ( ) } that orchestrates calling each
* of several abstract methods to be overriden by concrete implementations that allow for
* customizing how { @link Configuration } classes are found ( { @link # getConfigurationBeanDefinitions } ) ,
* customizing the creation of a { @link ConfigurationParser } ( { @link # createConfigurationParser } ) ,
* and customizing { @link ConfigurationModel } validation logic ( { @link # validateModel } ) .
*
* < p > This class was expressly designed with tooling in mind . Spring IDE will maintain it ' s
* own implementation of this class but still take advantage of the generic parsing algorithm
* defined here by { @link # processConfigBeanDefinitions ( ) } .
*
* @author Chris Beams
* @since 3 . 0
* @see ConfigurationClassPostProcessor
* /
public abstract class AbstractConfigurationClassProcessor {
/ * *
* Populate and return a registry containing all { @link Configuration } bean definitions
* to be processed .
*
* @param includeAbstractBeanDefs whether abstract Configuration bean definitions should
* be included in the resulting BeanDefinitionRegistry . Usually false , but called as true
* during the enhancement phase .
* @see # processConfigBeanDefinitions ( )
* /
protected abstract BeanDefinitionRegistry getConfigurationBeanDefinitions ( boolean includeAbstractBeanDefs ) ;
/ * *
* Create and return a new { @link ConfigurationParser } , allowing for customization of
* type ( ASM / JDT / Reflection ) as well as providing specialized ClassLoader during
* construction .
* @see # processConfigBeanDefinitions ( )
* /
protected abstract ConfigurationParser createConfigurationParser ( ) ;
/ * *
* Validate the given model and handle any errors . Implementations may choose to throw
* { @link MalformedConfigurationException } , or in the case of tooling register problems
* with the UI .
* @param configModel { @link ConfigurationModel } to validate
* /
protected abstract void validateModel ( ConfigurationModel configModel ) ;
protected BeanDefinitionRegistry processConfigBeanDefinitions ( ) {
/ * *
* Build and validate a { @link ConfigurationModel } based on the registry of
* { @link Configuration } classes provided by { @link # getConfigurationBeanDefinitions } ,
* then , based on the content of that model , create and register bean definitions
* against a new { @link BeanDefinitionRegistry } , then return the registry .
*
* @return registry containing one bean definition per { @link Bean } method declared
* within the Configuration classes
* /
protected final BeanDefinitionRegistry processConfigBeanDefinitions ( ) {
BeanDefinitionRegistry configBeanDefs = getConfigurationBeanDefinitions ( false ) ;
// return an empty registry immediately if no @Configuration classes were found
if ( configBeanDefs . getBeanDefinitionCount ( ) = = 0 )
return configBeanDefs ; // nothing to do - don't waste any more cycles
ConfigurationModel configModel = createConfigurationModelFor ( configBeanDefs ) ;
validateModel ( configModel ) ;
return configBeanDefs ;
return renderModelAsBeanDefinitions ( configModel ) ;
}
private ConfigurationModel createConfigurationModelFor ( BeanDefinitionRegistry configBeanDefinitions ) {
// populate a new ConfigurationModel by parsing each @Configuration classes
ConfigurationParser parser = createConfigurationParser ( ) ;
for ( String beanName : configBeanDefs . getBeanDefinitionNames ( ) ) {
BeanDefinition beanDef = configBeanDefs . getBeanDefinition ( beanName ) ;
String className = beanDef . getBeanClassName ( ) ;
parser . parse ( className , beanName ) ;
}
for ( String beanName : configBeanDefinitions . getBeanDefinitionNames ( ) ) {
BeanDefinition beanDef = configBeanDefinitions . getBeanDefinition ( beanName ) ;
String className = beanDef . getBeanClassName ( ) ;
parser . parse ( className , beanName ) ;
}
ConfigurationModel configModel = parser . getConfigurationModel ( ) ;
return parser . getConfigurationModel ( ) ;
}
private BeanDefinitionRegistry renderModelAsBeanDefinitions ( ConfigurationModel configModel ) {
// validate the ConfigurationModel
validateModel ( configModel ) ;
// read the model and create bean definitions based on its content
return new ConfigurationModelBeanDefinitionReader ( ) . loadBeanDefinitions ( configModel ) ;
}