From b17d545bb71b51ae46689a88487898dac496544c Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 9 Feb 2012 19:32:16 +0100 Subject: [PATCH] @ActiveProfiles mechanism works with @ImportResource as well (SPR-8992) --- ...onfigurationClassBeanDefinitionReader.java | 23 +++++--- .../ConfigurationClassPostProcessor.java | 6 +- .../DefaultProfileAnnotationConfigTests.java | 57 +++++++++++++++++++ .../importresource/DefaultProfileConfig.java | 37 ++++++++++++ .../DevProfileAnnotationConfigTests.java | 39 +++++++++++++ .../junit4/profile/importresource/import.xml | 13 +++++ 6 files changed, 164 insertions(+), 11 deletions(-) create mode 100644 org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/DefaultProfileAnnotationConfigTests.java create mode 100644 org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/DefaultProfileConfig.java create mode 100644 org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/DevProfileAnnotationConfigTests.java create mode 100644 org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/import.xml diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java index 7a333eb068a..a378e01787b 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -16,8 +16,6 @@ package org.springframework.context.annotation; -import static org.springframework.context.annotation.MetadataUtils.attributesFor; - import java.io.IOException; import java.lang.reflect.Method; import java.util.ArrayList; @@ -29,6 +27,7 @@ import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.annotation.Autowire; import org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor; @@ -46,6 +45,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.core.annotation.AnnotationAttributes; +import org.springframework.core.env.Environment; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.AnnotationMetadata; @@ -54,6 +54,8 @@ import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.util.StringUtils; +import static org.springframework.context.annotation.MetadataUtils.*; + /** * Reads a given fully-populated set of ConfigurationClass instances, registering bean * definitions with the given {@link BeanDefinitionRegistry} based on its contents. @@ -79,7 +81,10 @@ class ConfigurationClassBeanDefinitionReader { private final MetadataReaderFactory metadataReaderFactory; - private ResourceLoader resourceLoader; + private final ResourceLoader resourceLoader; + + private final Environment environment; + /** * Create a new {@link ConfigurationClassBeanDefinitionReader} instance that will be used @@ -89,13 +94,14 @@ class ConfigurationClassBeanDefinitionReader { */ public ConfigurationClassBeanDefinitionReader(BeanDefinitionRegistry registry, SourceExtractor sourceExtractor, ProblemReporter problemReporter, MetadataReaderFactory metadataReaderFactory, - ResourceLoader resourceLoader) { + ResourceLoader resourceLoader, Environment environment) { this.registry = registry; this.sourceExtractor = sourceExtractor; this.problemReporter = problemReporter; this.metadataReaderFactory = metadataReaderFactory; this.resourceLoader = resourceLoader; + this.environment = environment; } @@ -294,12 +300,12 @@ class ConfigurationClassBeanDefinitionReader { // Instantiate the specified BeanDefinitionReader BeanDefinitionReader readerInstance = readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry); - // Delegate the current ResourceLoader to it if possible if (readerInstance instanceof AbstractBeanDefinitionReader) { - ((AbstractBeanDefinitionReader)readerInstance).setResourceLoader(this.resourceLoader); + AbstractBeanDefinitionReader abdr = ((AbstractBeanDefinitionReader) readerInstance); + abdr.setResourceLoader(this.resourceLoader); + abdr.setEnvironment(this.environment); } - readerInstanceCache.put(readerClass, readerInstance); } catch (Exception ex) { @@ -354,6 +360,7 @@ class ConfigurationClassBeanDefinitionReader { * declare at least one {@link Bean @Bean} method. */ private static class InvalidConfigurationImportProblem extends Problem { + public InvalidConfigurationImportProblem(String className, Resource resource, AnnotationMetadata metadata) { super(String.format("%s was @Import'ed but is not annotated with @Configuration " + "nor does it declare any @Bean methods; it does not implement ImportSelector " + 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 919ab4f9952..8e724106de8 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-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -199,8 +199,8 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo private ConfigurationClassBeanDefinitionReader getConfigurationClassBeanDefinitionReader(BeanDefinitionRegistry registry) { if (this.reader == null) { - this.reader = new ConfigurationClassBeanDefinitionReader( - registry, this.sourceExtractor, this.problemReporter, this.metadataReaderFactory, this.resourceLoader); + this.reader = new ConfigurationClassBeanDefinitionReader(registry, this.sourceExtractor, + this.problemReporter, this.metadataReaderFactory, this.resourceLoader, this.environment); } return this.reader; } diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/DefaultProfileAnnotationConfigTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/DefaultProfileAnnotationConfigTests.java new file mode 100644 index 00000000000..dd0717bf105 --- /dev/null +++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/DefaultProfileAnnotationConfigTests.java @@ -0,0 +1,57 @@ +/* + * Copyright 2002-2012 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.test.context.junit4.profile.importresource; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.Employee; +import org.springframework.beans.Pet; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import static org.junit.Assert.*; + +/** + * @author Juergen Hoeller + * @since 3.1 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = DefaultProfileConfig.class, loader = AnnotationConfigContextLoader.class) +public class DefaultProfileAnnotationConfigTests { + + @Autowired + protected Pet pet; + + @Autowired(required = false) + protected Employee employee; + + + @Test + public void pet() { + assertNotNull(pet); + assertEquals("Fido", pet.getName()); + } + + @Test + public void employee() { + assertNull("employee bean should not be created for the default profile", employee); + } + +} diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/DefaultProfileConfig.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/DefaultProfileConfig.java new file mode 100644 index 00000000000..a4af38435a4 --- /dev/null +++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/DefaultProfileConfig.java @@ -0,0 +1,37 @@ +/* + * Copyright 2002-2012 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.test.context.junit4.profile.importresource; + +import org.springframework.beans.Pet; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; + +/** + * @author Juergen Hoeller + * @since 3.1 + */ +@Configuration +@ImportResource("org/springframework/test/context/junit4/profile/importresource/import.xml") +public class DefaultProfileConfig { + + @Bean + public Pet pet() { + return new Pet("Fido"); + } + +} diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/DevProfileAnnotationConfigTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/DevProfileAnnotationConfigTests.java new file mode 100644 index 00000000000..9e6ff731697 --- /dev/null +++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/DevProfileAnnotationConfigTests.java @@ -0,0 +1,39 @@ +/* + * Copyright 2002-2012 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.test.context.junit4.profile.importresource; + +import org.junit.Test; + +import org.springframework.test.context.ActiveProfiles; + +import static org.junit.Assert.*; + +/** + * @author Juergen Hoeller + * @since 3.1 + */ +@ActiveProfiles("dev") +public class DevProfileAnnotationConfigTests extends DefaultProfileAnnotationConfigTests { + + @Test + @Override + public void employee() { + assertNotNull("employee bean should be loaded for the 'dev' profile", employee); + assertEquals("John Smith", employee.getName()); + } + +} diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/import.xml b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/import.xml new file mode 100644 index 00000000000..8427a674a3d --- /dev/null +++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/importresource/import.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + +