Skip to content

Commit b17d545

Browse files
committed
@activeprofiles mechanism works with @ImportResource as well (SPR-8992)
1 parent 1bd260a commit b17d545

File tree

6 files changed

+164
-11
lines changed

6 files changed

+164
-11
lines changed

org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.context.annotation;
1818

19-
import static org.springframework.context.annotation.MetadataUtils.attributesFor;
20-
2119
import java.io.IOException;
2220
import java.lang.reflect.Method;
2321
import java.util.ArrayList;
@@ -29,6 +27,7 @@
2927

3028
import org.apache.commons.logging.Log;
3129
import org.apache.commons.logging.LogFactory;
30+
3231
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
3332
import org.springframework.beans.factory.annotation.Autowire;
3433
import org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor;
@@ -46,6 +45,7 @@
4645
import org.springframework.beans.factory.support.GenericBeanDefinition;
4746
import org.springframework.beans.factory.support.RootBeanDefinition;
4847
import org.springframework.core.annotation.AnnotationAttributes;
48+
import org.springframework.core.env.Environment;
4949
import org.springframework.core.io.Resource;
5050
import org.springframework.core.io.ResourceLoader;
5151
import org.springframework.core.type.AnnotationMetadata;
@@ -54,6 +54,8 @@
5454
import org.springframework.core.type.classreading.MetadataReaderFactory;
5555
import org.springframework.util.StringUtils;
5656

57+
import static org.springframework.context.annotation.MetadataUtils.*;
58+
5759
/**
5860
* Reads a given fully-populated set of ConfigurationClass instances, registering bean
5961
* definitions with the given {@link BeanDefinitionRegistry} based on its contents.
@@ -79,7 +81,10 @@ class ConfigurationClassBeanDefinitionReader {
7981

8082
private final MetadataReaderFactory metadataReaderFactory;
8183

82-
private ResourceLoader resourceLoader;
84+
private final ResourceLoader resourceLoader;
85+
86+
private final Environment environment;
87+
8388

8489
/**
8590
* Create a new {@link ConfigurationClassBeanDefinitionReader} instance that will be used
@@ -89,13 +94,14 @@ class ConfigurationClassBeanDefinitionReader {
8994
*/
9095
public ConfigurationClassBeanDefinitionReader(BeanDefinitionRegistry registry, SourceExtractor sourceExtractor,
9196
ProblemReporter problemReporter, MetadataReaderFactory metadataReaderFactory,
92-
ResourceLoader resourceLoader) {
97+
ResourceLoader resourceLoader, Environment environment) {
9398

9499
this.registry = registry;
95100
this.sourceExtractor = sourceExtractor;
96101
this.problemReporter = problemReporter;
97102
this.metadataReaderFactory = metadataReaderFactory;
98103
this.resourceLoader = resourceLoader;
104+
this.environment = environment;
99105
}
100106

101107

@@ -294,12 +300,12 @@ private void loadBeanDefinitionsFromImportedResources(
294300
// Instantiate the specified BeanDefinitionReader
295301
BeanDefinitionReader readerInstance =
296302
readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry);
297-
298303
// Delegate the current ResourceLoader to it if possible
299304
if (readerInstance instanceof AbstractBeanDefinitionReader) {
300-
((AbstractBeanDefinitionReader)readerInstance).setResourceLoader(this.resourceLoader);
305+
AbstractBeanDefinitionReader abdr = ((AbstractBeanDefinitionReader) readerInstance);
306+
abdr.setResourceLoader(this.resourceLoader);
307+
abdr.setEnvironment(this.environment);
301308
}
302-
303309
readerInstanceCache.put(readerClass, readerInstance);
304310
}
305311
catch (Exception ex) {
@@ -354,6 +360,7 @@ public ConfigurationClassBeanDefinition cloneBeanDefinition() {
354360
* declare at least one {@link Bean @Bean} method.
355361
*/
356362
private static class InvalidConfigurationImportProblem extends Problem {
363+
357364
public InvalidConfigurationImportProblem(String className, Resource resource, AnnotationMetadata metadata) {
358365
super(String.format("%s was @Import'ed but is not annotated with @Configuration " +
359366
"nor does it declare any @Bean methods; it does not implement ImportSelector " +

org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -199,8 +199,8 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
199199

200200
private ConfigurationClassBeanDefinitionReader getConfigurationClassBeanDefinitionReader(BeanDefinitionRegistry registry) {
201201
if (this.reader == null) {
202-
this.reader = new ConfigurationClassBeanDefinitionReader(
203-
registry, this.sourceExtractor, this.problemReporter, this.metadataReaderFactory, this.resourceLoader);
202+
this.reader = new ConfigurationClassBeanDefinitionReader(registry, this.sourceExtractor,
203+
this.problemReporter, this.metadataReaderFactory, this.resourceLoader, this.environment);
204204
}
205205
return this.reader;
206206
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit4.profile.importresource;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import org.springframework.beans.Employee;
23+
import org.springframework.beans.Pet;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.test.context.ContextConfiguration;
26+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
27+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
28+
29+
import static org.junit.Assert.*;
30+
31+
/**
32+
* @author Juergen Hoeller
33+
* @since 3.1
34+
*/
35+
@RunWith(SpringJUnit4ClassRunner.class)
36+
@ContextConfiguration(classes = DefaultProfileConfig.class, loader = AnnotationConfigContextLoader.class)
37+
public class DefaultProfileAnnotationConfigTests {
38+
39+
@Autowired
40+
protected Pet pet;
41+
42+
@Autowired(required = false)
43+
protected Employee employee;
44+
45+
46+
@Test
47+
public void pet() {
48+
assertNotNull(pet);
49+
assertEquals("Fido", pet.getName());
50+
}
51+
52+
@Test
53+
public void employee() {
54+
assertNull("employee bean should not be created for the default profile", employee);
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit4.profile.importresource;
18+
19+
import org.springframework.beans.Pet;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.context.annotation.ImportResource;
23+
24+
/**
25+
* @author Juergen Hoeller
26+
* @since 3.1
27+
*/
28+
@Configuration
29+
@ImportResource("org/springframework/test/context/junit4/profile/importresource/import.xml")
30+
public class DefaultProfileConfig {
31+
32+
@Bean
33+
public Pet pet() {
34+
return new Pet("Fido");
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit4.profile.importresource;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.test.context.ActiveProfiles;
22+
23+
import static org.junit.Assert.*;
24+
25+
/**
26+
* @author Juergen Hoeller
27+
* @since 3.1
28+
*/
29+
@ActiveProfiles("dev")
30+
public class DevProfileAnnotationConfigTests extends DefaultProfileAnnotationConfigTests {
31+
32+
@Test
33+
@Override
34+
public void employee() {
35+
assertNotNull("employee bean should be loaded for the 'dev' profile", employee);
36+
assertEquals("John Smith", employee.getName());
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
4+
5+
<beans profile="dev">
6+
<bean id="employee" class="org.springframework.beans.Employee">
7+
<property name="name" value="John Smith" />
8+
<property name="age" value="42" />
9+
<property name="company" value="Acme Widgets, Inc." />
10+
</bean>
11+
</beans>
12+
13+
</beans>

0 commit comments

Comments
 (0)