javagradlebuild.gradlemicronautmicronaut-rest

No bean of type [xxxx.xxxx.xxxx] exists. Make sure the bean is not disabled by bean requirements Micronaut messaging application


I have below project structure

enter image description here

The main project DEMO has a dependency on the Gradle project SERVICE which implements the interface from the PORT project.

Demo build.gradle

dependencies {
    implementation project(':port')
    runtime project(':service')
} 

Service build.gradle

dependencies {
    implementation project(':port')

    implementation("javax.annotation:javax.annotation-api:1.3.2")
    compile "io.micronaut:micronaut-inject:2.4.0"
    annotationProcessor "io.micronaut:micronaut-inject-java:2.4.0"
}

Service class implement IStartUpPort

@Singleton
public class StartUpService implements IStartUpPort {
    @Override
    public void toUpperCase() {
        System.out.println("Something happened");
    }
}

Port build.gradle

dependencies {
    implementation("javax.annotation:javax.annotation-api:1.3.2")
    compile "io.micronaut:micronaut-inject:2.4.0"
    annotationProcessor "io.micronaut:micronaut-inject-java:2.4.0"
}

Port interface

public interface IStartUpPort {
    void toUpperCase();
}

In the main project, I am doing the DI on a service project and facing the exception

@Singleton
public class StartUp implements ApplicationEventListener<StartupEvent> {
    private final IStartUpPort iStartUpPort;

    public StartUp(IStartUpPort iStartUpPort) {
        this.iStartUpPort = iStartUpPort;
    }

    @Override
    public void onApplicationEvent(StartupEvent event) {
        iStartUpPort.toUpperCase();
    }
}

Exception

Message: No bean of type [fete.bird.IStartUpPort] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
Path Taken: new StartUp([IStartUpPort iStartUpPort])
    at io.micronaut.context.AbstractBeanDefinition.getBeanForConstructorArgument(AbstractBeanDefinition.java:1034)
    at com.example.$StartUpDefinition.build(Unknown Source)
    at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1943)
    at io.micronaut.context.DefaultBeanContext.addCandidateToList(DefaultBeanContext.java:3091)
    at io.micronaut.context.DefaultBeanContext.getBeanRegistrations(DefaultBeanContext.java:2968)
    at io.micronaut.context.DefaultBeanContext.getBeansOfType(DefaultBeanContext.java:762)
    at io.micronaut.context.DefaultBeanContext.publishEvent(DefaultBeanContext.java:1304)
    at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:246)
    at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:165)
    at io.micronaut.runtime.Micronaut.start(Micronaut.java:71)
    at io.micronaut.runtime.Micronaut.run(Micronaut.java:311)
    at io.micronaut.runtime.Micronaut.run(Micronaut.java:297)
    at com.example.Application.main(Application.java:8)
Caused by: io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [fete.bird.IStartUpPort] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
    at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:2367)
    at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1261)
    at io.micronaut.context.AbstractBeanDefinition.getBeanForConstructorArgument(AbstractBeanDefinition.java:1012)
    ... 12 common frames omitted

This is a Micronuat messenging application repo can be found here https://github.com/anandjaisy/MessengingMultiGradleProject


Solution

  • The code currently in the linked repo is configured such that the error shown in the question will not happen if the app is built and executed properly.

    Clone the repository:

    $ git clone git@github.com:anandjaisy/MessengingMultiGradleProject.git
    

    Build the service:

    $ cd MessengingMultiGradleProject
    $ ./gradlew assemble
    

    Run the service:

    $ java -jar build/libs/demo-0.1-all.jar 
     __  __ _                                  _   
    |  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_ 
    | |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
    | |  | | | (__| | | (_) | | | | (_| | |_| | |_ 
    |_|  |_|_|\___|_|  \___/|_| |_|\__,_|\__,_|\__|
      Micronaut (v2.4.0)
    
    Something happened
    13:33:55.037 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 405ms. Server Running: 0 active message listeners.