Browse Source

Sets the child class loader to parent class loader.

This prevents odd behavior in jdk 11 +

Fixes https://github.com/spring-cloud/spring-cloud-netflix/issues/3101
pull/484/head
Spencer Gibb 6 years ago
parent
commit
b38ce54410
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 3
      spring-cloud-context/src/main/java/org/springframework/cloud/context/named/NamedContextFactory.java
  2. 8
      spring-cloud-context/src/test/java/org/springframework/cloud/context/named/NamedContextFactoryTests.java

3
spring-cloud-context/src/main/java/org/springframework/cloud/context/named/NamedContextFactory.java

@ -128,6 +128,9 @@ public abstract class NamedContextFactory<C extends NamedContextFactory.Specific
if (this.parent != null) { if (this.parent != null) {
// Uses Environment from parent as well as beans // Uses Environment from parent as well as beans
context.setParent(this.parent); context.setParent(this.parent);
// jdk11 issue
// https://github.com/spring-cloud/spring-cloud-netflix/issues/3101
context.setClassLoader(this.parent.getClassLoader());
} }
context.setDisplayName(generateDisplayName(name)); context.setDisplayName(generateDisplayName(name));
context.refresh(); context.refresh();

8
spring-cloud-context/src/test/java/org/springframework/cloud/context/named/NamedContextFactoryTests.java

@ -19,6 +19,7 @@ package org.springframework.cloud.context.named;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map; import java.util.Map;
import org.assertj.core.api.Assertions;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -65,6 +66,13 @@ public class NamedContextFactoryTests {
AnnotationConfigApplicationContext fooContext = factory.getContext("foo"); AnnotationConfigApplicationContext fooContext = factory.getContext("foo");
AnnotationConfigApplicationContext barContext = factory.getContext("bar"); AnnotationConfigApplicationContext barContext = factory.getContext("bar");
then(fooContext.getClassLoader())
.as("foo context classloader does not match parent")
.isSameAs(parent.getClassLoader());
Assertions.assertThat(fooContext).hasFieldOrPropertyWithValue("customClassLoader",
true);
factory.destroy(); factory.destroy();
then(fooContext.isActive()).as("foo context wasn't closed").isFalse(); then(fooContext.isActive()).as("foo context wasn't closed").isFalse();

Loading…
Cancel
Save