Refactor Environment and PropertySource
* Environment now extends PropertyResolver * Environment no longer exposes resolver and sources * PropertySource is String,Object instead of String,String * PropertySource no longer assumes enumerability of property names * Introduced EnumerablePropertySource for those that do have enumerable property names
This commit is contained in:
@@ -114,8 +114,8 @@ public class AnnotatedBeanDefinitionReader {
|
||||
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
|
||||
AnnotationMetadata metadata = abd.getMetadata();
|
||||
|
||||
if (Profile.Helper.isProfileAnnotationPresent(metadata)) {
|
||||
if (!this.environment.acceptsProfiles(Profile.Helper.getCandidateProfiles(metadata))) {
|
||||
if (ProfileHelper.isProfileAnnotationPresent(metadata)) {
|
||||
if (!this.environment.acceptsProfiles(ProfileHelper.getCandidateProfiles(metadata))) {
|
||||
// TODO SPR-7508: log that this bean is being rejected on profile mismatch
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
|
||||
|
||||
|
||||
/**
|
||||
* Create a new AnnotationConfigApplicationContext that needs to be populated
|
||||
* Create a new AnnotationConfigApplicationContext that needs to be populated
|
||||
* through {@link #register} calls and then manually {@linkplain #refresh refreshed}.
|
||||
*/
|
||||
public AnnotationConfigApplicationContext() {
|
||||
|
||||
@@ -280,7 +280,7 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
|
||||
* @return the pattern specification to be used for package searching
|
||||
*/
|
||||
protected String resolveBasePackage(String basePackage) {
|
||||
return ClassUtils.convertClassNameToResourcePath(environment.getPropertyResolver().resolveRequiredPlaceholders(basePackage));
|
||||
return ClassUtils.convertClassNameToResourcePath(environment.resolveRequiredPlaceholders(basePackage));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -298,11 +298,11 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
|
||||
for (TypeFilter tf : this.includeFilters) {
|
||||
if (tf.match(metadataReader, this.metadataReaderFactory)) {
|
||||
AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
|
||||
if (!Profile.Helper.isProfileAnnotationPresent(metadata)) {
|
||||
if (!ProfileHelper.isProfileAnnotationPresent(metadata)) {
|
||||
return true;
|
||||
}
|
||||
// TODO SPR-7508: log that this bean is being rejected on profile mismatch
|
||||
return this.environment.acceptsProfiles(Profile.Helper.getCandidateProfiles(metadata));
|
||||
return this.environment.acceptsProfiles(ProfileHelper.getCandidateProfiles(metadata));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -102,8 +102,8 @@ class ConfigurationClassParser {
|
||||
|
||||
protected void processConfigurationClass(ConfigurationClass configClass) throws IOException {
|
||||
AnnotationMetadata metadata = configClass.getMetadata();
|
||||
if (this.environment != null && Profile.Helper.isProfileAnnotationPresent(metadata)) {
|
||||
if (!this.environment.acceptsProfiles(Profile.Helper.getCandidateProfiles(metadata))) {
|
||||
if (this.environment != null && ProfileHelper.isProfileAnnotationPresent(metadata)) {
|
||||
if (!this.environment.acceptsProfiles(ProfileHelper.getCandidateProfiles(metadata))) {
|
||||
// TODO SPR-7508: log that this bean is being rejected on profile mismatch
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.core.env.AbstractEnvironment;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
/**
|
||||
* Indicates that a component is eligible for registration when one or more {@linkplain #value
|
||||
@@ -73,21 +72,4 @@ public @interface Profile {
|
||||
*/
|
||||
String[] value();
|
||||
|
||||
|
||||
static class Helper {
|
||||
/**
|
||||
* Return whether the given metadata includes Profile information, whether directly or
|
||||
* through meta-annotation.
|
||||
*/
|
||||
static boolean isProfileAnnotationPresent(AnnotationMetadata metadata) {
|
||||
return metadata.isAnnotated(Profile.class.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the String[] of candidate profiles from {@link Profile#value()}.
|
||||
*/
|
||||
static String[] getCandidateProfiles(AnnotationMetadata metadata) {
|
||||
return (String[])metadata.getAnnotationAttributes(Profile.class.getName()).get("value");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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.
|
||||
* 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.context.annotation;
|
||||
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
class ProfileHelper {
|
||||
/**
|
||||
* Return whether the given metadata includes Profile information, whether directly or
|
||||
* through meta-annotation.
|
||||
*/
|
||||
static boolean isProfileAnnotationPresent(AnnotationMetadata metadata) {
|
||||
return metadata.isAnnotated(Profile.class.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the String[] of candidate profiles from {@link Profile#value()}.
|
||||
*/
|
||||
static String[] getCandidateProfiles(AnnotationMetadata metadata) {
|
||||
return (String[])metadata.getAnnotationAttributes(Profile.class.getName()).get("value");
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
@@ -48,7 +48,7 @@ public class EnvironmentAccessor implements PropertyAccessor {
|
||||
* environment.
|
||||
*/
|
||||
public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
|
||||
return new TypedValue(((Environment)target).getPropertyResolver().getProperty(name));
|
||||
return new TypedValue(((Environment)target).getProperty(name));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -119,7 +119,7 @@ public abstract class AbstractRefreshableConfigApplicationContext extends Abstra
|
||||
* @return the resolved file path
|
||||
*/
|
||||
protected String resolvePath(String path) {
|
||||
return this.getEnvironment().getPropertyResolver().resolveRequiredPlaceholders(path);
|
||||
return this.getEnvironment().resolveRequiredPlaceholders(path);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -25,6 +25,8 @@ import org.springframework.beans.factory.config.AbstractPropertyPlaceholderConfi
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.EnumerablePropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertiesPropertySource;
|
||||
@@ -111,7 +113,7 @@ public class PropertySourcesPlaceholderConfigurer extends AbstractPropertyPlaceh
|
||||
if (this.propertySources == null) {
|
||||
this.propertySources = new MutablePropertySources();
|
||||
if (this.environment != null) {
|
||||
this.propertySources.addAll(this.environment.getPropertySources());
|
||||
this.propertySources.addAll(((ConfigurableEnvironment)this.environment).getPropertySources());
|
||||
}
|
||||
try {
|
||||
PropertySource<?> localPropertySource =
|
||||
@@ -128,7 +130,18 @@ public class PropertySourcesPlaceholderConfigurer extends AbstractPropertyPlaceh
|
||||
}
|
||||
|
||||
this.propertyResolver = new PropertySourcesPropertyResolver(this.propertySources);
|
||||
this.processProperties(beanFactory, this.propertyResolver.asProperties());
|
||||
this.processProperties(beanFactory, asProperties(this.propertySources));
|
||||
}
|
||||
|
||||
public Properties asProperties(PropertySources propertySources) {
|
||||
Properties mergedProps = new Properties();
|
||||
java.util.List<PropertySource<?>> propertySourcesList = propertySources.asList();
|
||||
for (int i = propertySourcesList.size() -1; i >= 0; i--) {
|
||||
PropertySource<?> source = propertySourcesList.get(i);
|
||||
for (String key : ((EnumerablePropertySource<?>)source).getPropertyNames()) {
|
||||
mergedProps.put(key, source.getProperty(key));
|
||||
}
|
||||
}
|
||||
return mergedProps;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user