Skip to content

Commit ec49499

Browse files
committed
DATACMNS-492 - RepositoryConfigurationDelegate now expects an Environment.
Added a constructor to RepositoryConfigurationDelegate that takes an additional Environment instance to make sure we can equip ClasspathScanningCandidateComponentProviders with Environments to make sure they only find components matching the environment. Retain the old constructor by defaulting to the Environment the ResourceLoader potentially contains or even a StandardEnvironment in worse cases. This is primarily to not break existing clients. Those should be upgraded to use the new constructor, of course. Needed to add an additional guard in BeanDefinitionRegistrarSupport as Spring 3.2.8 fails to invoke EnvironmentAware (filed SPR-11744 for that). This should be removed once we upgrade to Spring 3.2.9. Related pull request: #80. Related tickets: DATACMNS-493, DATACMNS-494, SPR-11744.
1 parent 63e595b commit ec49499

File tree

6 files changed

+107
-13
lines changed

6 files changed

+107
-13
lines changed

src/main/java/org/springframework/data/repository/config/RepositoryBeanDefinitionBuilder.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2929
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3030
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
31+
import org.springframework.core.env.Environment;
3132
import org.springframework.core.io.ResourceLoader;
3233
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
3334
import org.springframework.core.type.classreading.MetadataReaderFactory;
@@ -40,14 +41,15 @@
4041
*
4142
* @author Oliver Gierke
4243
*/
43-
public class RepositoryBeanDefinitionBuilder {
44+
class RepositoryBeanDefinitionBuilder {
4445

4546
private static final Logger LOGGER = LoggerFactory.getLogger(RepositoryBeanDefinitionBuilder.class);
4647
private static final String CUSTOM_IMPLEMENTATION_RESOURCE_PATTERN = "**/*%s.class";
4748

4849
private final BeanDefinitionRegistry registry;
4950
private final RepositoryConfigurationExtension extension;
5051
private final ResourceLoader resourceLoader;
52+
private final Environment environment;
5153

5254
private final MetadataReaderFactory metadataReaderFactory;
5355

@@ -58,16 +60,20 @@ public class RepositoryBeanDefinitionBuilder {
5860
* @param registry must not be {@literal null}.
5961
* @param extension must not be {@literal null}.
6062
* @param resourceLoader must not be {@literal null}.
63+
* @param environment must not be {@literal null}.
6164
*/
6265
public RepositoryBeanDefinitionBuilder(BeanDefinitionRegistry registry, RepositoryConfigurationExtension extension,
63-
ResourceLoader resourceLoader) {
66+
ResourceLoader resourceLoader, Environment environment) {
6467

65-
Assert.notNull(extension);
68+
Assert.notNull(extension, "RepositoryConfigurationExtension must not be null!");
69+
Assert.notNull(resourceLoader, "ResourceLoader must not be null!");
70+
Assert.notNull(environment, "Environement must not be null!");
6671

6772
this.registry = registry;
6873
this.extension = extension;
6974
this.resourceLoader = resourceLoader;
7075
this.metadataReaderFactory = new CachingMetadataReaderFactory(resourceLoader);
76+
this.environment = environment;
7177
}
7278

7379
/**
@@ -154,6 +160,7 @@ private AbstractBeanDefinition detectCustomImplementation(RepositoryConfiguratio
154160

155161
// Build classpath scanner and lookup bean definition
156162
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
163+
provider.setEnvironment(environment);
157164
provider.setResourceLoader(resourceLoader);
158165
provider.setResourcePattern(String.format(CUSTOM_IMPLEMENTATION_RESOURCE_PATTERN, className));
159166
provider.setMetadataReaderFactory(metadataReaderFactory);

src/main/java/org/springframework/data/repository/config/RepositoryBeanDefinitionParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public BeanDefinition parse(Element element, ParserContext parser) {
6262
ResourceLoader resourceLoader = parser.getReaderContext().getResourceLoader();
6363

6464
XmlRepositoryConfigurationSource configSource = new XmlRepositoryConfigurationSource(element, parser, environment);
65-
RepositoryConfigurationDelegate delegate = new RepositoryConfigurationDelegate(configSource, resourceLoader);
65+
RepositoryConfigurationDelegate delegate = new RepositoryConfigurationDelegate(configSource, resourceLoader,
66+
environment);
6667

6768
for (BeanComponentDefinition definition : delegate.registerRepositoriesIn(parser.getRegistry(), extension)) {
6869
parser.registerBeanComponent(definition);

src/main/java/org/springframework/data/repository/config/RepositoryBeanDefinitionRegistrarSupport.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.springframework.context.ResourceLoaderAware;
2424
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
2525
import org.springframework.core.env.Environment;
26+
import org.springframework.core.env.EnvironmentCapable;
27+
import org.springframework.core.env.StandardEnvironment;
2628
import org.springframework.core.io.ResourceLoader;
2729
import org.springframework.core.type.AnnotationMetadata;
2830
import org.springframework.util.Assert;
@@ -71,13 +73,34 @@ public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanD
7173
return;
7274
}
7375

76+
// TODO: remove once SPR-11744 gets fixed
77+
Environment environment = defaultEnvironment(this.environment, resourceLoader);
78+
7479
AnnotationRepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(
7580
annotationMetadata, getAnnotation(), resourceLoader, environment);
7681

77-
RepositoryConfigurationDelegate delegate = new RepositoryConfigurationDelegate(configurationSource, resourceLoader);
82+
RepositoryConfigurationDelegate delegate = new RepositoryConfigurationDelegate(configurationSource, resourceLoader,
83+
environment);
7884
delegate.registerRepositoriesIn(registry, getExtension());
7985
}
8086

87+
/**
88+
* Defaults the environment in case the given one is null. TODO: remove, once SPR-11744 gets fixed.
89+
*
90+
* @param environment
91+
* @param resourceLoader
92+
* @return
93+
*/
94+
private static Environment defaultEnvironment(Environment environment, ResourceLoader resourceLoader) {
95+
96+
if (environment != null) {
97+
return environment;
98+
}
99+
100+
return resourceLoader instanceof EnvironmentCapable ? ((EnvironmentCapable) resourceLoader).getEnvironment()
101+
: new StandardEnvironment();
102+
}
103+
81104
/**
82105
* Return the annotation to obtain configuration information from. Will be wrappen into an
83106
* {@link AnnotationRepositoryConfigurationSource} so have a look at the constants in there for what annotation

src/main/java/org/springframework/data/repository/config/RepositoryConfigurationDelegate.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2626
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2727
import org.springframework.beans.factory.support.BeanNameGenerator;
28+
import org.springframework.core.env.Environment;
29+
import org.springframework.core.env.EnvironmentCapable;
30+
import org.springframework.core.env.StandardEnvironment;
2831
import org.springframework.core.io.ResourceLoader;
2932
import org.springframework.util.Assert;
3033

@@ -42,6 +45,7 @@ public class RepositoryConfigurationDelegate {
4245

4346
private final RepositoryConfigurationSource configurationSource;
4447
private final ResourceLoader resourceLoader;
48+
private final Environment environment;
4549
private final BeanNameGenerator generator;
4650
private final boolean isXml;
4751

@@ -51,9 +55,24 @@ public class RepositoryConfigurationDelegate {
5155
*
5256
* @param configurationSource must not be {@literal null}.
5357
* @param resourceLoader must not be {@literal null}.
58+
* @deprecated use constructor taking an {@link Environment}.
5459
*/
60+
@Deprecated
5561
public RepositoryConfigurationDelegate(RepositoryConfigurationSource configurationSource,
5662
ResourceLoader resourceLoader) {
63+
this(configurationSource, resourceLoader, null);
64+
}
65+
66+
/**
67+
* Creates a new {@link RepositoryConfigurationDelegate} for the given {@link RepositoryConfigurationSource} and
68+
* {@link ResourceLoader} and {@link Environment}.
69+
*
70+
* @param configurationSource must not be {@literal null}.
71+
* @param resourceLoader must not be {@literal null}.
72+
* @param environment must not be {@literal null}.
73+
*/
74+
public RepositoryConfigurationDelegate(RepositoryConfigurationSource configurationSource,
75+
ResourceLoader resourceLoader, Environment environment) {
5776

5877
this.isXml = configurationSource instanceof XmlRepositoryConfigurationSource;
5978
boolean isAnnotation = configurationSource instanceof AnnotationRepositoryConfigurationSource;
@@ -68,6 +87,25 @@ public RepositoryConfigurationDelegate(RepositoryConfigurationSource configurati
6887
this.generator = generator;
6988
this.configurationSource = configurationSource;
7089
this.resourceLoader = resourceLoader;
90+
this.environment = defaultEnvironment(environment, resourceLoader);
91+
}
92+
93+
/**
94+
* Defaults the environment in case the given one is null. Used as fallback, in case the legacy constructor was
95+
* invoked.
96+
*
97+
* @param environment can be {@literal null}.
98+
* @param resourceLoader can be {@literal null}.
99+
* @return
100+
*/
101+
private static Environment defaultEnvironment(Environment environment, ResourceLoader resourceLoader) {
102+
103+
if (environment != null) {
104+
return environment;
105+
}
106+
107+
return resourceLoader instanceof EnvironmentCapable ? ((EnvironmentCapable) resourceLoader).getEnvironment()
108+
: new StandardEnvironment();
71109
}
72110

73111
/**
@@ -82,7 +120,8 @@ public List<BeanComponentDefinition> registerRepositoriesIn(BeanDefinitionRegist
82120

83121
extension.registerBeansForRoot(registry, configurationSource);
84122

85-
RepositoryBeanDefinitionBuilder builder = new RepositoryBeanDefinitionBuilder(registry, extension, resourceLoader);
123+
RepositoryBeanDefinitionBuilder builder = new RepositoryBeanDefinitionBuilder(registry, extension, resourceLoader,
124+
environment);
86125
List<BeanComponentDefinition> definitions = new ArrayList<BeanComponentDefinition>();
87126

88127
for (RepositoryConfiguration<? extends RepositoryConfigurationSource> configuration : extension

src/main/java/org/springframework/data/repository/config/RepositoryConfigurationSourceSupport.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.core.env.StandardEnvironment;
2626
import org.springframework.core.io.ResourceLoader;
2727
import org.springframework.core.type.filter.TypeFilter;
28+
import org.springframework.util.Assert;
2829

2930
/**
3031
* Base class to implement {@link RepositoryConfigurationSource}s.
@@ -42,10 +43,12 @@ public abstract class RepositoryConfigurationSourceSupport implements Repository
4243
* Creates a new {@link RepositoryConfigurationSourceSupport} with the given environment. Defaults to a plain
4344
* {@link StandardEnvironment} in case the given argument is {@literal null}.
4445
*
45-
* @param environment nullable, defaults to a {@link StandardEnvironment}.
46+
* @param environment must not be {@literal null}.
4647
*/
4748
public RepositoryConfigurationSourceSupport(Environment environment) {
48-
this.environment = environment == null ? new StandardEnvironment() : environment;
49+
50+
Assert.notNull(environment, "Environment must not be null!");
51+
this.environment = environment;
4952
}
5053

5154
/*

src/test/java/org/springframework/data/repository/config/RepositoryBeanDefinitionRegistrarSupportIntegrationTests.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2014 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.
@@ -18,12 +18,16 @@
1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
2020

21+
import org.junit.After;
22+
import org.junit.Before;
2123
import org.junit.Test;
2224
import org.springframework.beans.factory.config.BeanDefinition;
2325
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2426
import org.springframework.context.annotation.Configuration;
2527

2628
/**
29+
* Integration tests for {@link RepositoryBeanDefinitionRegistrarSupport}.
30+
*
2731
* @author Oliver Gierke
2832
*/
2933
public class RepositoryBeanDefinitionRegistrarSupportIntegrationTests {
@@ -39,20 +43,37 @@ static class TestConfig extends SampleConfig {
3943

4044
}
4145

46+
AnnotationConfigApplicationContext context;
47+
48+
@Before
49+
public void setUp() {
50+
this.context = new AnnotationConfigApplicationContext(TestConfig.class);
51+
}
52+
53+
@After
54+
public void tearDown() {
55+
56+
if (context != null) {
57+
this.context.close();
58+
}
59+
}
60+
61+
/**
62+
* @see DATACMNS-47
63+
*/
4264
@Test
4365
public void testBootstrappingWithInheritedConfigClasses() {
4466

45-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);
46-
4767
assertThat(context.getBean(MyRepository.class), is(notNullValue()));
4868
assertThat(context.getBean(MyOtherRepository.class), is(notNullValue()));
4969
}
5070

71+
/**
72+
* @see DATACMNS-47
73+
*/
5174
@Test
5275
public void beanDefinitionSourceIsSetForJavaConfigScannedBeans() {
5376

54-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);
55-
5677
BeanDefinition definition = context.getBeanDefinition("myRepository");
5778
assertThat(definition.getSource(), is(notNullValue()));
5879
}

0 commit comments

Comments
 (0)