diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfiguration.java index 2e0267763660..eaf6b1eeaeaa 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. @@ -29,6 +29,7 @@ import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; /** * Configuration for Hazelcast client. @@ -49,12 +50,15 @@ class HazelcastClientConfiguration { static class HazelcastClientConfigFileConfiguration { @Bean - HazelcastInstance hazelcastInstance(HazelcastProperties properties) throws IOException { - Resource config = properties.resolveConfigLocation(); - if (config != null) { - return new HazelcastClientFactory(config).getHazelcastInstance(); - } - return HazelcastClient.newHazelcastClient(); + HazelcastInstance hazelcastInstance(HazelcastProperties properties, ResourceLoader resourceLoader) + throws IOException { + return createHazelcastClientFactory(properties.resolveConfigLocation()) + .getHazelcastInstance(resourceLoader.getClassLoader()); + } + + private HazelcastClientFactory createHazelcastClientFactory(Resource config) throws IOException { + return (config != null) ? new HazelcastClientFactory(config) + : new HazelcastClientFactory(ClientConfig.load()); } } @@ -65,6 +69,7 @@ static class HazelcastClientConfigConfiguration { @Bean HazelcastInstance hazelcastInstance(ClientConfig config) { + // TODO should we replace the ClassLoader here? return new HazelcastClientFactory(config).getHazelcastInstance(); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientFactory.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientFactory.java index b1c97535143c..da93afe2b9cd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientFactory.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. @@ -67,10 +67,22 @@ private ClientConfig getClientConfig(Resource clientConfigLocation) throws IOExc } /** - * Get the {@link HazelcastInstance}. + * Get the {@link HazelcastInstance} with the default {@link ClassLoader}. * @return the {@link HazelcastInstance} */ public HazelcastInstance getHazelcastInstance() { + return getHazelcastInstance(null); + } + + /** + * Get the {@link HazelcastInstance} with the specified {@link ClassLoader}. + * @param classLoader the classloader to use for serialization + * @return the {@link HazelcastInstance} + */ + public HazelcastInstance getHazelcastInstance(ClassLoader classLoader) { + if (classLoader != null) { + this.clientConfig.setClassLoader(classLoader); + } if (StringUtils.hasText(this.clientConfig.getInstanceName())) { return HazelcastClient.getOrCreateHazelcastClient(this.clientConfig); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastInstanceFactory.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastInstanceFactory.java index be66d2dffb4a..5506e8a02b50 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastInstanceFactory.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastInstanceFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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. @@ -81,10 +81,22 @@ private static Config createConfig(URL configUrl) throws IOException { } /** - * Get the {@link HazelcastInstance}. + * Get the {@link HazelcastInstance} with the default {@link ClassLoader}. * @return the {@link HazelcastInstance} */ public HazelcastInstance getHazelcastInstance() { + return getHazelcastInstance(null); + } + + /** + * Get the {@link HazelcastInstance} with the specified {@link ClassLoader}. + * @param classLoader the classloader to use for serialization + * @return the {@link HazelcastInstance} + */ + public HazelcastInstance getHazelcastInstance(ClassLoader classLoader) { + if (classLoader != null) { + this.config.setClassLoader(classLoader); + } if (StringUtils.hasText(this.config.getInstanceName())) { return Hazelcast.getOrCreateHazelcastInstance(this.config); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java index 0a552bb19d90..bc15a888374b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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. @@ -19,7 +19,6 @@ import java.io.IOException; import com.hazelcast.config.Config; -import com.hazelcast.core.Hazelcast; import com.hazelcast.core.HazelcastInstance; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -28,6 +27,7 @@ import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; /** * Configuration for Hazelcast server. @@ -47,12 +47,15 @@ class HazelcastServerConfiguration { static class HazelcastServerConfigFileConfiguration { @Bean - HazelcastInstance hazelcastInstance(HazelcastProperties properties) throws IOException { - Resource config = properties.resolveConfigLocation(); - if (config != null) { - return new HazelcastInstanceFactory(config).getHazelcastInstance(); - } - return Hazelcast.newHazelcastInstance(); + HazelcastInstance hazelcastInstance(HazelcastProperties properties, ResourceLoader resourceLoader) + throws IOException { + return createHazelcastInstanceFactory(properties.resolveConfigLocation()) + .getHazelcastInstance(resourceLoader.getClassLoader()); + } + + private HazelcastInstanceFactory createHazelcastInstanceFactory(Resource config) throws IOException { + return (config != null) ? new HazelcastInstanceFactory(config) + : new HazelcastInstanceFactory(Config.load()); } } @@ -63,6 +66,7 @@ static class HazelcastServerConfigConfiguration { @Bean HazelcastInstance hazelcastInstance(Config config) { + // TODO should we replace the ClassLoader here? return new HazelcastInstanceFactory(config).getHazelcastInstance(); }