Skip to content

krud-dev/spring-componentmap

Repository files navigation


spring-componentmap

Awesome Kotlin Maven Central CircleCI Codecov GitHub contributions welcome

Overview

spring-componentmap is a JVM library that provides a simple way to inject a map of beans by type.

The library allows both the injection of a map of beans by type and the injection of a map of lists of beans by type.

Documentation

To learn how to get started with spring-componentmap, visit the official documentation website. You'll find in-depth documentation, tips and guides to help you get up and running.

Click here

Example

Head to the Examples directory to see usage examples.

Installation

Requirements

  • Minimum supported Java version: 1.8
  • Spring Boot 2.5.0+

Maven

<dependency>
  <groupId>dev.krud</groupId>
  <artifactId>spring-componentmap</artifactId>
  <version>0.1.0</version>
</dependency>

Gradle

Groovy DSL

implementation 'dev.krud:spring-componentmap:0.1.0'

Kotlin DSL

implementation("dev.krud:spring-componentmap:0.1.0")

Setup

Simply add the dependency to your Spring Boot project, Spring Boot will then run the ComponentMapAutoConfiguration class and register the ComponentMapBeanPostProcessor bean.

Usage

To use the @ComponentMap annotation, simply add it to a Map field and Spring Boot will automatically populate it with all beans of the specified type.

Kotlin Example

@Component
class ActionHandlerMap {
    /**
     * The `@ComponentMap` annotation will automatically populate this map with all beans of type `ActionHandler`
     */
    @ComponentMap 
    private lateinit var handlers: Map<String, ActionHandler>
    
    fun handle(type: String) {
        handlers[type]?.handle()
    }
}

interface ActionHandler {
    /**
     * The action that this handler can handle, add the `@ComponentMapKey` annotation to the getter in order to register it
     */
    @get:ComponentMapKey
    val type: String
    fun handle()
}

@Component
class ActionHandler1 : ActionHandler {
    override val type = "type1"
    override fun handle() {
        println("ActionHandler1")
    }
}

@Component
class ActionHandler2 : ActionHandler {
    override val type = "type2"
    override fun handle() {
        println("ActionHandler2")
    }
}

Java Example

@Component
public class ActionHandlerMap {
    /**
     * The `@ComponentMap` annotation will automatically populate this map with all beans of type `ActionHandler`
     */
    @ComponentMap 
    private Map<String, ActionHandler> handlers;
    
    public void handle(String type) {
        handlers.get(type).handle();
    }
}

public interface ActionHandler {
    /**
     * The action that this handler can handle, add the `@ComponentMapKey` annotation to the getter in order to register it
     */
    @ComponentMapKey
    String getType();
    void handle();
}

@Component
public class ActionHandler1 implements ActionHandler {
    @Override
    public String getType() {
        return "type1";
    }

    @Override
    public void handle() {
        System.out.println("ActionHandler1");
    }
}

@Component
public class ActionHandler2 implements ActionHandler {
    @Override
    public String getType() {
        return "type2";
    }

    @Override
    public void handle() {
        System.out.println("ActionHandler2");
    }
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. See CONTRIBUTING.md for more information.

License

spring-componentmap is licensed under the MIT license. For more information, please see the LICENSE file.

About

JVM library for simple injection of maps of beans.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published