How to find a particular class object from a List of Object in Spring boot Dependency Injection

Subhendu Mondal :

I am creating a Plugin system, where the plugin will be fetched from DB, my Implementation is like below

A base Plugin Interface

@Service
public interface Plugin{
   somemethod();
}

In plugin package load all the plugin where inner package name will be plugin name and a class with plugin name

package <>.plugin.Myplugin

import ....

@Service 
class MyPlugin implements Plugin {
   @Override
   somemethod() {}
}

I am loading all the implemented class of Plugin interface in dependency injection

@Controller
public class MyCon {

   private List<Plugin> plugins

   public MyCon(List<Plugin> plugins) {
        this.plugins = plugins;
   }

   ssomefuncitonality() {

        String objectNeedOfClass = <CLASS_NAME>;
        //Here
        System.out.println(this.plugins);
   }
}

Here can see all the object in the list, but how can I fetch the perticular object of the provided class Name.

Andreas :

Iterate the list and check the class name:

Optional<Plugin> plugin = this.plugins.stream()
        .filter(p -> p.getClass().getSimpleName().equals(objectNeedOfClass))
        .findFirst();

If plugins can be large and you do this often, build a map in the constructor:

private Map<String, Plugin> plugins

public MyCon(List<Plugin> plugins) {
    this.plugins = plugins.stream().collect(Collectors.toMap(
            p -> p.getClass().getSimpleName(), Function.identity()));
}

ssomefuncitonality() {
    String objectNeedOfClass = ...;
    Plugin plugin = this.plugins.get(objectNeedOfClass);
    ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Dependency injection and serialization Object in spring

Spring Java Object Dependency Injection

How to get Array List from an Object of Lists in Java Spring Boot?

Spring boot, how to read particular object from json file, what annotation do I need?

spring boot dependency injection

how to do dependency injection manually in spring boot

Dependency Injection - Create object from interface

How to use dependency injection to inject object into module?

How to pass an object to a ViewModel using dependency injection?

How to map from JPA Object in spring boot

How to return the particular field from Spring Optional instead of the whole object?

How to make efficient list from particular object in kotlin

How to add particular ids object from list using Java?

Spring boot application: how to trace exact starter which dependency particular jar came from?

Spring Boot Application - How can I pass data (list in list) from controller to Thymeleaf based on current object?

How do I best use an object factory inside another class using Pimple for Dependency Injection?

Dependency injection with abstract class and object in Play Framework 2.5

Play Framework dependency injection Object vs @Singleton Class

Scala dependency injection when using case class/companion object pattern

How to apply server side validation on List of Object in spring boot? spring not validating List<Address> which is present in Employee Class

Mapstruct dependency injection using componentModel = "spring" gives null object

How to find an object from array list

How to find (and remove) a nested object from a List

How to import a class object from a list of objects?

How to remove a class object from a python list?

Class not mapping from Get API Request in Spring Boot, how to map to object>

Spring dependency injection and generic class

Dependency Injection object creation clarification

Pimple dependency injection static or object