From f18fa08aa05e2cd35601f739a4e989d2320aee3c Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 18 Jul 2011 20:45:02 +0000 Subject: [PATCH] ConfigurationClassPostProcessor supports use of same processor instance with several factories (SPR-8527) --- .../ConfigurationClassPostProcessor.java | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java index 1f9c45d0e19..ee30dffd532 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 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. @@ -17,6 +17,7 @@ package org.springframework.context.annotation; import java.io.IOException; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; @@ -80,9 +81,9 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo private boolean setMetadataReaderFactoryCalled = false; - private boolean postProcessBeanDefinitionRegistryCalled = false; + private final Set registriesPostProcessed = new HashSet(); - private boolean postProcessBeanFactoryCalled = false; + private final Set factoriesPostProcessed = new HashSet(); /** @@ -130,15 +131,16 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo * Derive further bean definitions from the configuration classes in the registry. */ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) { - if (this.postProcessBeanDefinitionRegistryCalled) { + int registryId = System.identityHashCode(registry); + if (this.registriesPostProcessed.contains(registryId)) { throw new IllegalStateException( - "postProcessBeanDefinitionRegistry already called for this post-processor"); + "postProcessBeanDefinitionRegistry already called for this post-processor against " + registry); } - if (this.postProcessBeanFactoryCalled) { + if (this.factoriesPostProcessed.contains(registryId)) { throw new IllegalStateException( - "postProcessBeanFactory already called for this post-processor"); + "postProcessBeanFactory already called for this post-processor against " + registry); } - this.postProcessBeanDefinitionRegistryCalled = true; + this.registriesPostProcessed.add(registryId); processConfigBeanDefinitions(registry); } @@ -147,12 +149,13 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo * by replacing them with CGLIB-enhanced subclasses. */ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { - if (this.postProcessBeanFactoryCalled) { + int factoryId = System.identityHashCode(beanFactory); + if (this.factoriesPostProcessed.contains(factoryId)) { throw new IllegalStateException( - "postProcessBeanFactory already called for this post-processor"); + "postProcessBeanFactory already called for this post-processor against " + beanFactory); } - this.postProcessBeanFactoryCalled = true; - if (!this.postProcessBeanDefinitionRegistryCalled) { + this.factoriesPostProcessed.add((factoryId)); + if (!this.registriesPostProcessed.contains((factoryId))) { // BeanDefinitionRegistryPostProcessor hook apparently not supported... // Simply call processConfigBeanDefinitions lazily at this point then. processConfigBeanDefinitions((BeanDefinitionRegistry) beanFactory);