Skip to content

Commit

Permalink
Fix the NPE occurred when using EnableApolloConfig with Spring 3.1.1 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodyiam authored Dec 26, 2021
1 parent 7c6c535 commit 9a666c8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Apollo 2.0.0
* [fix gray publish refresh item status](https://github.com/apolloconfig/apollo/pull/4128)
* [Support only show difference keys when compare namespace](https://github.com/apolloconfig/apollo/pull/4165)
* [Fix the issue that property placeholder doesn't work for dubbo reference beans](https://github.com/apolloconfig/apollo/pull/4175)
* [Fix the NPE occurred when using EnableApolloConfig with Spring 3.1.1](https://github.com/apolloconfig/apollo/pull/4180)

------------------
All issues and pull requests are [here](https://github.com/ctripcorp/apollo/milestone/8?closed=1)
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@
import com.google.common.collect.Lists;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

public class DefaultApolloConfigRegistrarHelper implements ApolloConfigRegistrarHelper {
private static final Logger logger = LoggerFactory.getLogger(
DefaultApolloConfigRegistrarHelper.class);

private Environment environment;

Expand Down Expand Up @@ -62,6 +66,11 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
}

private String[] resolveNamespaces(String[] namespaces) {
// no support for Spring version prior to 3.2.x, see https://github.com/apolloconfig/apollo/issues/4178
if (this.environment == null) {
logNamespacePlaceholderNotSupportedMessage(namespaces);
return namespaces;
}
String[] resolvedNamespaces = new String[namespaces.length];
for (int i = 0; i < namespaces.length; i++) {
// throw IllegalArgumentException if given text is null or if any placeholders are unresolvable
Expand All @@ -70,6 +79,17 @@ private String[] resolveNamespaces(String[] namespaces) {
return resolvedNamespaces;
}

private void logNamespacePlaceholderNotSupportedMessage(String[] namespaces) {
for (String namespace : namespaces) {
if (namespace.contains("${")) {
logger.warn("Namespace placeholder {} is not supported for Spring version prior to 3.2.x,"
+ " see https://github.com/apolloconfig/apollo/issues/4178 for more details.",
namespace);
break;
}
}
}

@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
Expand Down

0 comments on commit 9a666c8

Please sign in to comment.