Skip to content

Commit

Permalink
ConfigurationPropertiesScan should account for conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhave committed Apr 23, 2019
1 parent f908e3a commit 9fb65e5
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.stereotype.Component;
Expand All @@ -39,7 +43,12 @@
*
* @author Madhura Bhave
*/
class ConfigurationPropertiesScanRegistrar implements ImportBeanDefinitionRegistrar {
class ConfigurationPropertiesScanRegistrar
implements ImportBeanDefinitionRegistrar, EnvironmentAware, ResourceLoaderAware {

private Environment environment;

private ResourceLoader resourceLoader;

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
Expand Down Expand Up @@ -72,6 +81,8 @@ protected void scan(Set<String> packages, ConfigurableListableBeanFactory beanFa
BeanDefinitionRegistry registry) {
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
false);
scanner.setEnvironment(this.environment);
scanner.setResourceLoader(this.resourceLoader);
scanner.addIncludeFilter(new AnnotationTypeFilter(ConfigurationProperties.class));
for (String basePackage : packages) {
if (StringUtils.hasText(basePackage)) {
Expand Down Expand Up @@ -111,4 +122,14 @@ private void validateScanConfiguration(Class<?> type) {
}
}

@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}

@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2012-2019 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
*
* https://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.boot.context.properties;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import org.springframework.boot.context.properties.scan.valid.a.AScanConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.DefaultResourceLoader;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willCallRealMethod;

/**
* Integration tests for {@link ConfigurationPropertiesScan}.
*
* @author Madhura Bhave
*/
public class ConfigurationPropertiesScanTests {

private AnnotationConfigApplicationContext context;

@Before
public void setup() {
this.context = new AnnotationConfigApplicationContext();
}

@After
public void teardown() {
if (this.context != null) {
this.context.close();
}
}

@Test
public void scanImportBeanRegistrarShouldBeEnvironmentAware() {
// this.context.getEnvironment().addActiveProfile("test");
load(TestConfiguration.class);
assertThat(this.context.containsBean(
"profile-org.springframework.boot.context.properties.scan.valid.a.AScanConfiguration$MyProfileProperties"))
.isTrue();
}

@Test
public void scanImportBeanRegistrarShouldBeResourceLoaderAware() {
DefaultResourceLoader resourceLoader = Mockito.mock(DefaultResourceLoader.class);
this.context.setResourceLoader(resourceLoader);
willCallRealMethod().given(resourceLoader).getClassLoader();
given(resourceLoader.getResource("test"))
.willReturn(new ByteArrayResource("test".getBytes()));
load(TestConfiguration.class);
assertThat(this.context.containsBean(
"resource-org.springframework.boot.context.properties.scan.valid.a.AScanConfiguration$MyResourceProperties"))
.isTrue();
}

private void load(Class<?>... classes) {
this.context.register(classes);
this.context.refresh();
}

@ConfigurationPropertiesScan(basePackageClasses = AScanConfiguration.class)
static class TestConfiguration {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
package org.springframework.boot.context.properties.scan.valid.a;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
* @author Madhura Bhave
Expand All @@ -27,4 +33,27 @@ static class AProperties {

}

@Profile("test")
@ConfigurationProperties(prefix = "profile")
static class MyProfileProperties {

}

@Conditional(TestResourceCondition.class)
@ConfigurationProperties(prefix = "resource")
static class MyResourceProperties {

}

static class TestResourceCondition implements Condition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
ByteArrayResource resource = (ByteArrayResource) context.getResourceLoader()
.getResource("test");
return (new String(resource.getByteArray())).equals("test");
}

}

}

0 comments on commit 9fb65e5

Please sign in to comment.