Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
import java.util.Properties;

import org.apache.commons.configuration.AbstractConfiguration;
import com.netflix.config.DynamicPropertyFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.Ordered;

import com.netflix.config.ConfigurationManager;
import com.netflix.config.DynamicPropertyFactory;

public class ConfigurationSpringInitializer extends PropertyPlaceholderConfigurer {
public ConfigurationSpringInitializer() {
ConfigUtil.installDynamicConfig();
setOrder(Ordered.LOWEST_PRECEDENCE / 2);
setIgnoreUnresolvablePlaceholders(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* 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 io.servicecomb.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

// do not implements PriorityOrdered, must be Ordered
// because this bean must run after PropertyPlaceholderConfigurer
// this class's purpose: when asked to resolve placeholder, then throw exception directly
@Component
public class LastPropertyPlaceholderConfigurer implements BeanFactoryPostProcessor, Ordered {
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
new PropertyPlaceholderConfigurer().postProcessBeanFactory(beanFactory);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.core.Ordered;

import com.netflix.config.ConfigurationManager;

import io.servicecomb.config.archaius.sources.MicroserviceConfigLoader;
import mockit.Deencapsulation;

public class TestConfigurationSpringInitializer {
@BeforeClass
Expand All @@ -41,7 +43,11 @@ public static void classTeardown() {

@Test
public void testAll() {
new ConfigurationSpringInitializer();
ConfigurationSpringInitializer configurationSpringInitializer = new ConfigurationSpringInitializer();

Assert.assertEquals(Ordered.LOWEST_PRECEDENCE / 2, configurationSpringInitializer.getOrder());
Assert.assertEquals(true,
Deencapsulation.getField(configurationSpringInitializer, "ignoreUnresolvablePlaceholders"));

Object o = ConfigUtil.getProperty("zq");
@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* 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 io.servicecomb.config;

import java.io.IOException;
import java.util.Properties;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

public class TestLastPropertyPlaceholderConfigurer {
@Component
static class Bean extends PropertyPlaceholderConfigurer implements EmbeddedValueResolverAware {
StringValueResolver resolver;

public Bean() {
setIgnoreUnresolvablePlaceholders(true);
}

@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.resolver = resolver;
}

@Override
protected Properties mergeProperties() throws IOException {
Properties properties = super.mergeProperties();
properties.put("a", "aValue");
return properties;
}
}

@Test
public void test() {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(this.getClass().getPackage().getName());
Bean bean = context.getBean(Bean.class);

Assert.assertEquals("aValue", bean.resolver.resolveStringValue("${a}"));
try {
bean.resolver.resolveStringValue("${b}");
Assert.fail("must throw exception");
} catch (IllegalArgumentException e) {
Assert.assertEquals("Could not resolve placeholder 'b' in string value \"${b}\"", e.getMessage());
}

context.close();
}
}