Search code examples
micronaut

How to make Micronaut create Beans from a custom Library?


I tried the steps as described in the following article

Or same as in this GitHub post

It didn't work for me.

I also read this approach: See here But this is not what I am searching for

How can I customize my Library in a way, to make Micronaut capable of creating and injecting its beans directly in my project?


Solution

  • How can I customize my Library in a way, to make Micronaut capable of creating and injecting its beans directly in my project?

    If you want to customize the library, compile it with a dependency on the appropriate compile time annotation processors and then consume that .jar in your Micronaut app the same way you would any other .jar. plugins.gradle.org/plugin/io.micronaut.library is helpful for that if you are using Gradle.

    If you are consuming a library for which you can't do that, the @Factory annotation is specifically for this.

    @Factory
    class MyFactory {
    
        @Singleton
        SomeLibraryType someMethod() {
            // this could contain any appropriate
            // initialization code...
            return new SomeLibraryType();
        }
    }
    

    See docs.micronaut.io/3.4.2/guide/#factories.