This is an automated email from the ASF dual-hosted git repository.

namelchev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new 85295e12bf2 IGNITE-17311 Added Spring bean loader for given names and 
classes (#10139)
85295e12bf2 is described below

commit 85295e12bf2a549979f23328751375fbb8b3892a
Author: Nikita Amelchev <[email protected]>
AuthorDate: Thu Jul 7 12:25:24 2022 +0300

    IGNITE-17311 Added Spring bean loader for given names and classes (#10139)
---
 .../internal/util/spring/IgniteSpringHelper.java   | 15 ++++++
 .../util/spring/IgniteSpringHelperImpl.java        | 53 ++++++++++++++--------
 2 files changed, 50 insertions(+), 18 deletions(-)

diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelper.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelper.java
index cddef6506a4..94ce6631903 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelper.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelper.java
@@ -25,6 +25,7 @@ import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.IgniteLogger;
 import org.apache.ignite.configuration.IgniteConfiguration;
 import 
org.apache.ignite.internal.processors.resource.GridSpringResourceContext;
+import org.apache.ignite.internal.util.lang.GridTuple3;
 import org.apache.ignite.lang.IgniteBiTuple;
 
 /**
@@ -103,6 +104,20 @@ public interface IgniteSpringHelper {
         Class<?>... beanClasses
     ) throws IgniteCheckedException;
 
+    /**
+     * Loads bean instances that match the given types or names from given 
configuration file.
+     *
+     * @param cfgUrl Configuration file path or URL. This cannot be {@code 
null}.
+     * @param beanClasses Beans classes.
+     * @return Tuple containing all loaded beans and Spring context used to 
load them.
+     * @throws IgniteCheckedException If failed to load configuration.
+     */
+    public GridTuple3<Map<String, ?>, Map<Class<?>, Collection>, ? extends 
GridSpringResourceContext> loadBeans(
+        URL cfgUrl,
+        Collection<String> beanNames,
+        Class<?>... beanClasses
+    ) throws IgniteCheckedException;
+
     /**
      * Loads bean instance by name.
      *
diff --git 
a/modules/spring/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelperImpl.java
 
b/modules/spring/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelperImpl.java
index 3963d1f2f2f..e67e5b4ca0f 100644
--- 
a/modules/spring/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelperImpl.java
+++ 
b/modules/spring/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelperImpl.java
@@ -33,6 +33,7 @@ import org.apache.ignite.IgniteLogger;
 import org.apache.ignite.configuration.IgniteConfiguration;
 import 
org.apache.ignite.internal.processors.resource.GridSpringResourceContext;
 import 
org.apache.ignite.internal.processors.resource.GridSpringResourceContextImpl;
+import org.apache.ignite.internal.util.lang.GridTuple3;
 import org.apache.ignite.internal.util.typedef.F;
 import org.apache.ignite.internal.util.typedef.X;
 import org.apache.ignite.internal.util.typedef.internal.U;
@@ -151,6 +152,29 @@ public class IgniteSpringHelperImpl implements 
IgniteSpringHelper {
 
         ApplicationContext springCtx = initContext(cfgUrl);
 
+        Map<Class<?>, Collection> beans = loadBeans(springCtx, beanClasses);
+
+        return F.t(beans, new GridSpringResourceContextImpl(springCtx));
+    }
+
+    /** {@inheritDoc} */
+    @Override public GridTuple3<Map<String, ?>, Map<Class<?>, Collection>, ? 
extends GridSpringResourceContext> loadBeans(
+        URL cfgUrl,
+        Collection<String> beanNames,
+        Class<?>... beanClasses
+    ) throws IgniteCheckedException {
+        ApplicationContext springCtx = initContext(cfgUrl);
+
+        Map<String, ?> beansByName = new HashMap<>();
+
+        for (String name : beanNames)
+            beansByName.put(name, loadBean(springCtx, name));
+
+        return F.t(beansByName, loadBeans(springCtx, beanClasses), new 
GridSpringResourceContextImpl(springCtx));
+    }
+
+    /** Loads bean instances that match the given types. */
+    private Map<Class<?>, Collection> loadBeans(ApplicationContext springCtx, 
Class<?>... beanClasses) {
         Map<Class<?>, Collection> beans = new HashMap<>();
 
         for (Class<?> cls : beanClasses) {
@@ -160,11 +184,10 @@ public class IgniteSpringHelperImpl implements 
IgniteSpringHelper {
                 beans.put(cls, clsBeans.values());
         }
 
-        return F.t(beans, new GridSpringResourceContextImpl(springCtx));
+        return beans;
     }
 
     /** {@inheritDoc} */
-    @SuppressWarnings("unchecked")
     @Override public <T> T loadBean(URL url, String beanName) throws 
IgniteCheckedException {
         ApplicationContext springCtx = initContext(url);
 
@@ -182,37 +205,31 @@ public class IgniteSpringHelperImpl implements 
IgniteSpringHelper {
     }
 
     /** {@inheritDoc} */
-    @SuppressWarnings("unchecked")
     @Override public <T> T loadBean(InputStream cfgStream, String beanName) 
throws IgniteCheckedException {
         ApplicationContext springCtx = initContext(cfgStream);
 
-        try {
-            return (T)springCtx.getBean(beanName);
-        }
-        catch (NoSuchBeanDefinitionException ignored) {
-            throw new IgniteCheckedException("Spring bean with provided name 
doesn't exist " +
-                ", beanName=" + beanName + ']');
-        }
-        catch (BeansException e) {
-            throw new IgniteCheckedException("Failed to load Spring bean with 
provided name " +
-                ", beanName=" + beanName + ']', e);
-        }
+        return loadBean(springCtx, beanName);
     }
 
     /** {@inheritDoc} */
     @Override public <T> T loadBeanFromAppContext(Object appContext, String 
beanName) throws IgniteCheckedException {
         ApplicationContext springCtx = (ApplicationContext)appContext;
 
+        return loadBean(springCtx, beanName);
+    }
+
+    /** Loads bean instance that match the given name. */
+    private <T> T loadBean(ApplicationContext springCtx, String beanName) 
throws IgniteCheckedException {
         try {
             return (T)springCtx.getBean(beanName);
         }
         catch (NoSuchBeanDefinitionException ignored) {
-            throw new IgniteCheckedException("Spring bean with provided name 
doesn't exist " +
-                ", beanName=" + beanName + ']');
+            throw new IgniteCheckedException("Spring bean with provided name 
doesn't exist [beanName=" +
+                beanName + ']');
         }
         catch (BeansException e) {
-            throw new IgniteCheckedException("Failed to load Spring bean with 
provided name " +
-                ", beanName=" + beanName + ']', e);
+            throw new IgniteCheckedException("Failed to load Spring bean with 
provided name [beanName=" +
+                beanName + ']', e);
         }
     }
 

Reply via email to